Search for Python strings

Source: Internet
Author: User

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:

 
 
  1. 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:

 
 
  1. 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 ].

 
 
  1. 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.

 
 
  1. S.rindex(substr, [start, [end]])   
  2. S.count(substr, [start, [end]])  

The above article describes how to search and replace Python String Conversion functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.