Getting Started with Python's string
First, the concept and create a string of
1, the concept of string
* In the program, a text string expressed
* String is an ordered series of characters, such as: 'helloworld'
* Strings and lists, tuples, as are all sequences of type '
* Can be seen as a list of strings of characters, a list of many operations are also applicable for strings
* There is no separate character type, the character is a string containing one element, for example: 'a', 'b', 'c'
2, create a string of
'' ' Double or single quotes may create a string ' '' S = 'OK' Print (S, type (S)) # OK <class 'STR'> S1 = "OK1" Print (S1, type ( S1)) # OK1 <class 'str'> '' ' using the built-in function str, the incoming can be numbers, letters may be, may be a float, the data type is the final type str ' '' S2 = str ( 'ABC') Print (S2, type (S2)) # ABC <class 'STR'> S3 = STR ( '123') Print (S3, type (S3)) # 123 <class 'STR'>
Second, the escape character
1, using the escape character can not be represented by special characters
When the string contains line breaks, when special characters can not be represented directly carriage return, horizontal tab, backspace, etc., how to express it?
Wrap: newline, the cursor moves to the beginning of the next line
Enter: return, move the cursor to the beginning of the line
Horizontal tab: tab key, the cursor moves to the next group of four spaces at the beginning
Backspace: backspace key, go back one character
You can use the following escape character
Wrap: \ n
Enter: \ r
Horizontal tab: \ t
Backspace: \ b
print ( 'abc \ ndef') # abcdef shown with a line print ( 'abc \ rdef') # def carriage, moving the cursor to the beginning of the Bank print ( '123456 \ t123 \ t45 ') # 123456 123 45 tabs 4 space, according to the overall length of the character count print ( 'abc \ bdef') # abdef backspace, delete the c
2, a backslash '\', as an escape character
For example to print directly from a string, but would comprise a single lead in the string, the double lead, etc., need to \ escape
Print ( 'What \' S you name ') # apos What you name Print ( "print a double quote \"') to print a double quote # " Print ( '\\ print a backslash') to print a backslash # \
3, raw strings
For example, want to print '\ tC: \ Program Files' -t to tabs, do not want -t take effect, you can use the original string raw
print('\t:programfiles') # :programfiles print(r'\t:programfiles') #\t:programfiles print(R'\t:programfiles') #\t:programfiles
4, the character string search operation
a) the use of index, rindex, find, rfind lookup string element index
= S '12,334,567' Print (s.index ( '. 3')) # 2 Print (s.rindex ( '. 3')). 3 # Print (s.find ( '. 3')) # 2 Print (s.rfind ( '3')) # 3 #, and STOP can be specified search start, for example, starting from the end of the index 1-5, the lookup element index 3 Print (s.index ( '3', l, 5)) # 2 Print (S. rfind ( '3', 1,5) ) # 3 # when the element is not looking at the given index, index and rindex method throws value error when the element # when looking out of the specified index, find, and the method returns rfind -1 Print (s.index ( '. 9', l, 5)) # a ValueError: Not found the substring Print (s.rindex ( '. 9', l, 5)) # a ValueError: Not found the substring Print (s.find ( '. 9', l, 5)) # -1 Print (s.rfind ( '. 9', l, 5)) # -1
b) may also be used to find the list of search operation, searching for a corresponding index element
s = 'Python' print (s [3]) # h print (s [1: 4]) # Dear print (s [: - 1]) # Pytho print (s [:: - 1]) # nohtyP print ( s [:]) # Python print (s [: 2: -1]) # noh
5, the string comparison
a) using>, <, ==,! = string comparison
s1 = 'abc' s2 = 'def' print(s1 == s2) # False print(s1[0] < s2[0]) # True print(s1[1] > s2[0]) # False print(s1 != s2) # True
b) a string to be applied is, == is equality comparison, IS is relatively identical, but For strings, repeated calls Python
a = b = '123' c = '123' print (a is b) #True print (a == c) #True print (a is c) #True print(id(a),id(c)) #139917133452656 139917133452656
6, the string is reversed
Using the built-in function reversed string is inverted
s = 'hello world' print(list(reversed(s))) # ['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']
7, of the sort string
Sorted using the built-in function to sort strings
= S 'EdfaCb' # collation can be specified, for example, be converted to lower case, and then sorted or sorted strings End inverting # is not specified rules, according to the ord () sort of way print (sorted (s, key str.lower =)) # [ 'A', 'B', 'C', 'D', 'E', 'F'] Print (the sorted (S, Reverse = True)) # [ 'F', ' D ',' B ',' A ',' E ',' C '] Print (the sorted (S)) # [' C ',' E ',' A ',' B ',' D ',' F ']
8, case conversion of the string
a) upper Converts all characters to uppercase
b) lower converting all characters into lower case
c) swapcase all lowercase to uppercase, lowercase uppercase turn
d) title to the beginning of each word to uppercase
s = 'java PytHon Shell' print(s.lower()) #java python shell print(s.upper()) # JAVA PYTHON SHELL print(s.swapcase()) # JAVA pYThON sHELL print(s.title()) # Java Python Shell
9、字符串的对齐
a) center 中心对齐
b) rjust 右对齐
c) ljust 左对齐
以上方法可以指定两个参数,第一个参数是宽度,第二个参数是对齐符号,不指定第二个参数默认是空格。
d) zfill:右对齐,左边用0填充
该方法值接收一个参数,用于指定字符的宽度,如果指定的字符宽度小于字符串本身,那么返回字符串本身
s = 'hello world' print(s.center(20,'*')) # ****hello world***** print(s.ljust(18,'^')) # hello world^^^^^^^ print(s.rjust(18,'$')) # $$$$$$$hello world print(s.zfill(15)) # 0000hello world
10、字符串的替换
调用replace方法,对字符串进行替换,str.replace('old','new','replace_num')
s = 'hi hi hi hello' #将hi替换为hello,最大替换个数为2个,不指定替换个数默认为全部 print(s.replace('hi','hello',2)) # hello hello hi hello
11、去除字符串的前导符和后导符
调用方法lstrip、rstrip、strip对字符串前导符和后导符进行去除
s = '****hello world^^^^^' print(s.lstrip('*')) # hello world^^^^^ print(s.rstrip('^')) # ****hello world print(s.strip('*^')) # hello world