Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to search and replace in Python string

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to search and replace the Python string, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Python string type conversion functions, which are only available in the string module:

String.atoi (s [, base])

# base defaults to 10, if it is 0, then s can be a string in the form of 012 or 0x23, if it is 16, then s can only be a string in the form of 0x23 or 0X12 string.atol (s [, base]) # converted to long string.atof (s [, base]) # converted to float

Again, the string object is immutable, which means that after python creates a string, you cannot change part of the character. After any of the above functions change the string, it returns a new string, and the original string remains the same. In fact, there is a workaround. You can use the function S=list (S) to change S into a list with a single character as a member, so that you can use S [3] ='a'to change the value, and then use S = ". Join (S) to restore to a string.

Search and replace in Python string:

S.find (substr, [start, [end]])

# returns the label of the * letter of substr in S, or-1 if there is no substr in S. The role of start and end is equivalent to searching in S [start: end]

S.index (substr, [start, [end]])

# is the same as find (), except that if there is no substr in S, it returns a runtime error # returns the label of * of the substr in S, and returns-1 if there is no substr in S, that is, the first letter label of the substr that appears for the * times from the right

S.rindex (substr, [start, [end]]) S.count (substr, [start, [end]]) after reading the above, have you mastered how to search and replace in Python strings? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report