In a wide range of applications, Python strings involve the conversion functions of Python string types, and the code of related functions in the string module, the following is an introduction to the relevant content of the article. I hope you will learn from it.
Python string type conversion functions, which are only available in the string module:
- string.atoi(s[,base])
# Base is 10 by default. If it is 0, s can be a string in the format of 012 or 0x23. If it is 16, s can only be a string in the format of 0x23 or 0X12. atol (s [, base]) # converts it to long string. atof (s [, base]) # converts it to float
Once again, the string object cannot be changed. That is to say, after creating a string in python, you cannot change a part of the character. After any of the above functions change the string, a new string is returned, and the original string is not changed. In fact, this is also a work und. You can use the S = list (S) function to change S to a list with a single character as a member, in this way, you can use S [3] = 'A' to change the value, and then use S = "". restore join (S) to a string
Search and replace Python strings:
- S.find(substr, [start, [end]])
# Return the number of the first letter of substr in S. If S does not contain substr,-1 is returned. The functions of start and end are equivalent to searching in S [start: end ].
- S.index(substr, [start, [end]])
# Same as find (), but returns a runtime error when S does not have substr # returns the label of the first letter of the last substr in S, if there is no substr in S,-1 is returned, that is, the first letter of the substr that appears for the first time from the right.
- S.rindex(substr, [start, [end]])
- S.count(substr, [start, [end]])
The above article describes how to search and replace Python String Conversion functions.