Python-based strings and python-based strings
Str string
The python string 'str' is the most common basic data type in programming in Python.
A. Create A string
A = str ('string ')
A = 'This is also a string'
Astr = 'I am string' # use single quotes or double quotes to create bint = 123 # This is an int type bstr = str (bint) # str () can be used to List, dict, convert int and float classes into strings # print (astr, type (astr) print (bint, type (bint) print (bstr, type (bstr ))
# Output
I am a string <class 'str'>
123 <class 'int'>
123 <class 'str'>
B. Three quotation marks """"""
1. Define a string
2. Explanation
3. Document string, mainly used in the class (calss), using _ doc __
Atstr = "Here I can write my arrangement in this way, so that I can arrange the output in this way """
Output result:
Here I can write my arrangement in this way, and the output will be like this. How can I arrange the output?
C. escape characters with backslash (\) in python
Escape Character Description \ (at the end of a row) line break \ backslash \ 'single quotation marks \ "Double quotation marks \ a bell \ B Backspace) \ e escape \ 000 blank \ n line feed \ v vertical tab \ t horizontal tab \ r press enter \ f form feed \ oyy octal number, yy represents the characters, for example: \ o12 indicates the newline \ xyy hexadecimal number, and yy indicates the characters. For example, \ x0a indicates the newline \ other. other characters are output in normal format.
1. original string
Use r to represent the original string, for example: (r 'C: \ Python \ Python36 \ scripts ')
D. format the string.
1.% formatting character
Print ('my name is % s % d this year! '% ('Xiaoming', 10) # output result: My name is James, 10 years old!
Character Description % c formatting character and its ASCII code % s formatting string % d formatting integer % u formatting unsigned integer % o formatting unsigned octal number % x formatting unsigned hexadecimal number % X format the unsigned hexadecimal number (uppercase) % f format a floating point number. The precision % e after the decimal point can be specified. The scientific notation is used to format the Floating Point Number % E, use scientific notation to format the address of the variable in % g % f and % e in the notation % G % f and % E in the hexadecimal format.
2. format () format {} (braces)
Print ('My name is {} years old! '. Format ('xiaoming', 10) # output result: My name is James, 10 years old!
E. String operators astr = 'holle' and bstr = 'python'
1. + String connection astr + bstr
Cstr = astr + bstrprint (cstr) # output result: HollePython
2. * repeated output string astr * 3
Print (astr * 3) # output result: HolleHolleHolle
3. [] obtain the character astr [0] In the string through the index
Print (astr [0]) # output result: H
4. [:] truncate part of the string, astr [], from 1 to 4, excluding 4
Print (astr []) # output result: oll
Forward
Reverse
5. [:-1] Reverse string (-1 reverse)
Print (astr [:-1]) # output result: elloH
6. Judgment
6.1 in member operator-returns True if the string contains a given character
Print ('H' in astr) # output result: True
6.2. not in member operator-returns True if the string does not contain the given character
Print ('H' not in astr) # output result: False # -------------------------------- print ('A' not in astr) # output result: True
F. built-in string functions
1. strip () is used to remove the specified character (space by default) from the string header and tail, and strip ('symbol to be removed ')
A = '$ #$ # holle python' c =. strip () d = c. strip ('$ #') print (c) print (d) # output result: $ #$ # holle pythonholle python
1.1. lstrip () Remove the left space
A = 'lstrip' print (a. lstrip () # output result: lstrip
1.2.rstrip () Remove spaces on the right
A = 'lstrip' print (a. rstrip () # output result: lstrip
2. replace (x, y, z) is used to replace three characters. The first two parameters are required,
The first is the original substring to be replaced, the second is the new substring to be replaced, and the third is the maximum number of replicas. If this parameter is left blank, all replicas are replaced by default.
Dstr = 'I'm James, James's James' print (dstr. replace ('xiaoming ', 'xiaobai') print (dstr. replace ('xiao Ming ', 'xiao Bai', 2) # input the third parameter and replace it twice at most # output result: I am Xiao Bai, xiao Ming of Tom
3. index () and find () are used to check whether a string contains a substring and return the index position of the string.
3.1 search by index (). If it is found, the index location of the string is displayed.
Aindex = 'My name is Xiaoming 'print (aindex. index ('name') # output result: 2
3.2. If index () does not exist, an error is returned.
Aindex = 'My name is Xiaoming 'print (aindex. index ('others') # output result: ValueError: substring not found
3.3. find (). If it is found, the index location of the string is displayed (search from the left by default)
Aindex = 'My name is small x' print (aindex. find ('x') # output result: 6
3.4. rfind () is searched from the right. If it is found, the index location of the string is displayed.
Aindex = 'My name is small x' print (aindex. rfind ('x') # output result: 6
3.4. If find () and rfind () cannot be found,-1 is returned.
Aindex = 'My name is small x' print (aindex. find ('s') # output result:-1
E. String comparison
To use operator, import: from operator import lt, le, gt, ge, eq
Lt is equivalent to: <, le is equivalent to: <=, gt is equivalent to:>, ge is equivalent to:> =, eq is equivalent to: =
* Here we use a letter comparison. Starting from the 26 letters a, the first a is the smallest, and the later the bigger.
Ator = 'abc' btor = 'hij' print (lt (ator, btor) # output result: True
Print (le (ator, btor) # output result: True
Print (gt (ator, btor) # output result: False
Print (ge (ator, btor) # output result: False
Print (eq (ator, btor) # output result: False
F. String-to-letter Conversion
1. lowercase letters to uppercase upper ()
Abi = 'abcde' print (abi. upper () # convert lowercase letters to uppercase letters # output result: abcde
2. Convert uppercase letters to lowercase lower ()
Axi = 'abcde' print (abi. lower () # uppercase letters and lowercase letters # output result: ABCDE
3. title-UPPERCASE title ()
Axi = 'abcde' print (axi. title () # title, uppercase # output result: ABCDE
4. Case reversal --- convert lowercase to uppercase, and convert lowercase to swapcase ()
Abx = 'abcdefghijkl 'print (abx. swapcase () # output result: aBcdEfGHijKL
5. split the string and put it in a list () -----. split ()
Asp = 'a B c d E' print (asp. split () # output result: ['A', 'B', 'C', 'D', 'E']
5.1. split (','), split the string and put it into a list ()
Aspb = 'a, B, c, d, e' print (aspb. split () # output result: ['a, B, c, d, e']
6. join () concatenated strings
Ajo = 'abc' bjo = '$'. join (ajo) # use $ to connect to the print (bjo) string # output result: a $ B $ c $ d
7. String alignment: ljust (), just ust (), center ()
Ajust = 'python' print (ajust. ljust (50, '-') # align to the right with-complement 50 # output result: python --------------------------------------------
Print (ajust. Just ust (50, '-') # align with-50 to the left # output result: ---------------------------------------------- python
Print (ajust. center (50, '-') # align with-50 to the center # output result: ---------------------- python ----------------------
8. Determine the start and end characters of startswith () and endtswith ()
Asta = 'holle python' print (asta. startswith ('ho') # Start with ho # output result: True
Asta = 'holle python' print (asta. endswith ('on') # judge to end with on # output result: True
9. Calculate the number of times that the substring in the string appears count ()
Acob = 'Xi in xi cc bbi dd' print (acob. count ('xi') # calculate the number of times that xi appears in acob # output result: 2
10. zfill () returns a string of the specified length. The original string is aligned to the right, and the front is filled with 0.
Ajust = 'python' print (ajust. zfill (10) # Return a string with a length of 10 and fill it with 0 in the right alignment # output result: 000000000 python
# Click to view built-in functions for other Python strings.