[Python Study Notes] string manipulation

Source: Internet
Author: User

                     string Manipulation                                                                   

A. String formatted output
1 " Liu " 2 Print "  " % name3   4# output: I am Liu   6 PS: string is%s; integer%d; floating-point number%f
B. Finding a string (find)
1 # detects if STR is included in MyStr, or returns 1 if the index value is returned .

1 ' Xinge is so handsome. ' 2   3 >>> str.find ('xing')40 5   6 >>> str.find (' good ')7 6
C. Finding the contents of a replacement string (replace)
1 mystr.replace (str1, str2, Mystr.count (str1)) # replace mystr in str1 with str2 , if Count is specified, replace no more than count Times.

1 ' ABCABCABC ' 2   3 >>> str.replace ('a','xinge' , 2)4'xingebcxingebcabc'
d. Slicing with Str (split)
Mystr.split (str=""#  with Str as the delimiter slice mystr, if the maxsplit has a specified value, only the Maxsplit substring is delimited

1>>> str ='A\NB\TC'2  3>>>Str4 'A\NB\TC'5  6>>> Str.Split()7['a','b','C']
F. Capitalize the first letter of the string (capitalize)
1 >>> str='abc'2   3 >>>  Str.capitalize ()4'Abc'
G. Capitalize the first letter of each word of a string (title)
1 " Hello World " 2   3 >>> str.title ()4'HelloWorld'
H.startswith,endswith
1 startswith #  checks if the string starts with STR, returns TRUE, otherwise False2 endswith # checks whether the string ends with STR, or returns False if True.
I.upper,lower
1 Upper #  convert all characters in mystr to uppercase 2 Lower #  convert all characters in mystr to lowercase
J.strip,lstrip,rstrip
1 Strip #  clears the left and right spaces 2 lstrip #  clears the left space 3 Rstrip #  Clear the right space
K.join
1 mystr.join (str) #  mystr Insert Str After each character, constructing a new string

Welcome to add!

[Python Study Notes] string manipulation

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.