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