Python basic data type--str

Source: Internet
Author: User

First, the creation of a string
Test = str ()/"" "Test = str (" Licheng ")/" Licheng "
    • No arguments, creating an empty string
    • A parameter that creates a normal string
    • two parameters, int (bytes, encoded)
Second, the common method of the string
#capitalize (): string first character uppercase String = ' This is a string. ' New_str = String.capitalize () print (NEW_STR) #输出: This is a string. #center (width, fillchar=none): Place the string in the middle, and at the specified length, fill in string = ' This is a string with the specified character. ' New_str = String.center (+, ' * ') print (NEW_STR) #输出: ******this is a string.******* #count (sub, Start=none, End=none) : Computes the number of characters in a string = ' This is a string '. New_str = String.count (' i ') print (NEW_STR) #输出: 3 #decode/encode (Encoding=none, Errors=none): decode/decode string = ' This is a String. ' New_str = String.decode () New_str = String.encode () print (NEW_STR) #endswith (self, suffix, start=none, end=none) : Determines whether to end a character string = ' This is a string. ' New_str = String.endswith (' ing. ') Print (NEW_STR) #输出: True #find (self, sub, Start=none, End=none): Finds the position of the specified character in a string = ' This is a string '. New_str = String.find (' a ') #找的到的情况print (NEW_STR) #输出: 8new_str = String.find (' xx ') #找不到的情况返回 -1print (NEW_STR) #输出:-1 # Index (self, sub, Start=none, End=none): similar to findstring = ' This is a string. ' New_str = String.index (' a ') #找的到的情况print (NEW_STR) #输Out: 8new_str = String.index (' xx ') #找不到的情况, program error print (NEW_STR) #输出: program operation error, valueerror:substring not found #isalnum (self) : Determines whether the string is all numbers and letters, or returns TRUE if it is, otherwise returns falsestring = ' My name is yue,my 18. ' New_str = String.isalnum () print (NEW_STR) #输出: falsestring = ' Haha18121314lala ' new_str = String.isalnum () print (NEW_STR) #输出: True #isalpha (self): Determines whether the string is all letters, or returns TRUE if it is, otherwise falsestring = ' ABCDEFG ' new_str = String.isalpha () print (NEW_STR) #输出: truestring = ' My name is yue ' new_str = String.isalpha () #字母中间带空格, no special characters print (NEW_STR) #输出: False # isdigit (self) : Determines whether the string is a number, or returns True if it is, otherwise returns falsestring = ' 1234567890 ' new_str = String.isdigit () print (NEW_STR) #输出: truestring = ' Haha123lala ' new_str = String.isdigit () #中间带空格, no special characters print (NEW_STR) #输出: False # islower (self): Determines whether the letters in a string are lowercase, Returns true if it is, otherwise returns falsestring = ' My name is Yue,my 18. ' New_str = String.islower () print (NEW_STR) #输出: truestring = ' My name is Yue,my ' 18. ' New_str = String.islower () print (NEW_STR) #输出: False # Isupper (self): detects whether all letters in the string are uppercase. string = ' MY NAME is YUE. ' New_str = String.isupper () print (NEW_STR) #输出: truestring = ' My name is Yue. ' New_str = String.isupper () print (NEW_STR) #输出: False # join (self, iterable): Generates a new string for the element in the sequence with the specified character connection. String = ("haha", "lala", "Ohoh") str = "-" Print (Str.join (string)) #输出: Haha-lala-ohoh # lower (self): Converts all uppercase characters in a string to lowercase. String = "My Name is YUE." Print (String.Lower ()) # output: My name is Yue. # Lstrip (self, chars=none): Truncates the left space of the string or specifies the character. String = "My Name is YUE." Print (String.lstrip ()) #输出: My name is yue.string = "My name is YUE." Print (String.lstrip (' My)) #输出: Name is YUE. #replace (self, old, new, Count=none): replaces the older string in the string with the new one, and if you specify the third parameter max, the replacement does not exceed Max times. string = "My name is Yue." Print (String.Replace ("Yue", "Ying")) #输出: My name is Ying. # RFind (self, sub, Start=none, End=none): Returns the position of the last occurrence of the string, or 1 if there is no match. string = "My name is Yue." Print (String.rfind (' is ')) #输出: 8string = "My name is Yue." Print (String.rfind (' XXX ')) #输出: -1 # split (self, Sep=none, Maxsplit=none): Slices The string by specifying a delimiter. string = "haha lala gege" Print (string. Split (")) #输出: [' haha ', ' lala ', ' Gege ']print (String.Split (', 1)) #输出: [' haha ', ' lala Gege '] # rsplit (self, sep=none, MA Xsplit=none): Slices the string from right by specifying a delimiter. string = "haha lala gege" Print (String.rsplit (")) #输出: [' haha ', ' lala ', ' Gege ']print (String.rsplit (", 1)) #输出: [' Haha lal A ', ' Gege '] # Rstrip (self, chars=none): Deletes the specified character at the end of the string (the default is a space). String = "My name is Yue." Print (String.rstrip ()) #输出: My name is Yue. # strip (Self, chars=none): Removes the character specified by the tail of the string (the default is a space). string = "My name is Yue." Print (String.strip ()) #输出: My name is Yue. # upper (self): Converts lowercase letters in a string to uppercase. string = "My name is Yue,my 18." Print (String.upper ()) #输出: MY NAME is yue,my 18.
Str source Three, the public function of the string
    • Index (only one element can be taken)
    • Slices (take multiple elements)
    • Length (len)
      • Python2: Length by byte
      • Python3: Count length by character
    • For loop (version Loop unit of the same length)
Iv. conversion of characters to bytes
# convert GBK encoded characters to bytes s = "Li Cheng" b = bytes (s, encoding= "GBK") type (b)  output to byte type # converts bytes to characters C = str (b, encoding= "GBK")
Five, string formatting

There are two ways to format a Python string: a percent-semicolon, format-mode

The percent of the semicolon is relatively old, and format is a more advanced way to replace the old way, the two coexist.

1, percent of the way

%[(name)][flags][width]. [Precision]typecode
Detailed parameters

Common formatting:

TPL = "I am%s"% "spark" TPL = "I am%s age%d"% ("spark", +) TPL = "I am% (name) s age percent (age) d"% {"name": "Spark", "AG E ":" TPL = "percent%.2f"% 99.97623 TPL = "I am% (PP)." 2f "% {" pp ": 123.425556,} TPL =" I am%.2f percent "% {" pp ": 123.425 556,}

2. Format mode

[[Fill]align] [Sign] [#] [0] [Width] [,] [. Precision] [Type]
Detailed parameters

Common formatting:

 1 TPL = "I am {}, age {}, {}". Format ("Seven", +, ' Alex ') 2 3 TPL = "I am {}, age {}, {}". Format (*["Seven", "Alex" ]) 4 5 TPL = "I am {0}, age {1}, really {0}". Format ("seven", +) 6 7 TPL = "I am {0}, age {1}, really {0}". Format (*[  "Seven") 8 9 TPL = "I am {name}, age {age}, really {name}". Format (name= "Seven", age=18), TPL = "I am {name}", Age {Age}, really {name} '. Format (**{"name": "Seven", "Age": +}), TPL = "I am {0[0]}, age {0[1]}, really {0[2]}". fo Rmat ([1, 2, 3], [One, one, and all]) (= "I am {: s}, age {:d}," ". Format (" Seven ",, 88888.1) AM {: s}, age {:d} ". Format (*[" Seven ", [+]):" I am {name:s}, age {age:d} ". Format (name=" Seven ", AGE=18) TP L = "I am {name:s}, age {age:d}". Format (**{"name": "Seven", "Age": ()) = "Numbers: {: b},{:o},{:d},{:x},{:x}, {: %} ". Format (15.87623, 2, 15, +, +), TPL =" numbers: {: b},{:o},{:d},{:x},{:x}, {:%} ". Format (15,, 15, 15, 1 5, 15.87623, 2) TPL = "numbers: {0:b},{0:o},{0:d},{0:x},{0:x}, {0:%}". Format (page), TPL = "Numbers: {num:b},{num:o},{num:d},{num:x},{ Num:x}, {num:%} ". Format (NUM=15)

More formatting operations: https://docs.python.org/3/library/string.html

Python basic data type--str

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.