General Form of replacement of Python strings

Source: Internet
Author: User

In computer languages, Python strings are commonly used. In actual application, if you are confused about Python strings, the following is a detailed description of the application process.

Alignment of Python strings in output:

 
 
  1. S.ljust(width,[fillchar])   

# The output width is a string of characters, and the S is left aligned. The missing part is filled with fillchar. The default value is space.

 
 
  1. S.rjust(width,[fillchar]) #  

Right alignment

 
 
  1. S.center(width, [fillchar]) #  

In the center alignment S. zfill (width) # convert S to width length, and align it on the right. The missing part uses 0 to supplement the search and replacement in the string:

 
 
  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]])   

# It is the same as find (), but a runtime error is returned when S does not contain substr.

 
 
  1. S.rfind(substr, [start, [end]])   

# Return the number of the first letter of the last occurrence of substr in S. If there is no substr in S,-1 is returned, that is, the first letter of the first occurrence of substr from the right.

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

Calculates the number of times that substr appears in S.

 
 
  1. S.replace(oldstr, newstr, [count])   

# Replace oldstar with newstr in S, and count with the number of replicas. This is a common form of replacement, and some functions Replace special characters.

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.