Python string manipulation

Source: Internet
Author: User

First, string formatting

print ("%s%d%f"% ("a", 123, 12.33))for very long information can be output by using the three-quote "' Custom formatsuch as:msg= ""Information of%s:name:%sage:%sjob:%s"% (name, name, age, Job)Print (msg) second, the string commonly used functions:·Remove whitespace   · Split   · Length   · Index
· Slices     Remove whitespacename = input (' Input your Name: '). Strip ()Age = init (input (' Input your: '))Job = input ("Input your Job:"). Strip ()   string. Strip () can only remove the space between the middle of the strip () the default is to go to the space, of course, not only to remove the space, but also to remove other characters such as string.Strip ('% ') to remove intermediate percent signString Internal functionstr.__add__ (1) __add__ equivalent to + but only one parameter can be passed at a time name.__contains__ (' str ') is equivalent to ' str ' in namename.__eq__ equivalent to comparing two characters if the reflection is equal. __ GETATTRIBUTE__ () capitalize () uppercase Casefold () capitalize the lowercase center () to center the characters, specify how much I, and the plus characters can be filled as: result = Name.center (20, ' * ')  Count counts the number of occurrences of a string as name = ' Adsfasdfasdf ' Name.count (' DS ') can calculate the total number of DS in name and can also specify the starting position Name.count (' DS ', 0, 3) encode Encode the string for example: Name = ' Li Fangyuan ' result = Name.encode (' GBK ') Str.endswith () see if the string ends with >>>name = ' Lance ' >>> result = Name.endswith (' C ') >>>print (result) False>>>result = Name.endswith (' C ', 0, 3) >>> Print (Result) Truestartswith () See what started Str.expandtabs () the table is converted to a space to specify the number of spaces such as:>>> ' Ab\tc '. Expandtabs (4) ' AB C ' Str.find () finds the position that the character occupies, returns the first found position str.index () can also find the location where the string is the only difference is that the index finds a character that does not exist, and the Find () returns 1 str.swapcase () to lowercase. lowercase to uppercase str.title ()

Python string manipulation

Related Article

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.