What is a string
Everything wrapped in quotation marks is a string. In python, Chinese and English sentences need to be wrapped in quotation marks, otherwise an error will be reported
For example:
'A string wrapped in single quotes' "A string wrapped in double quotes" '''A string wrapped in three single quotes''' """A string wrapped in three double quotes""" Sentences without quotation marks will report errors
About three quotation marks
The contents of the three quotation marks are strings Three quotation marks can also be used as multiline comments(We mentioned in the last article)
String index (also known as subscript) [important, need to remember]
Strings are in order, starting from 0 from left to right. We can take values in order
str1 = 'abcdefg'# Define a string variable with the content of 'abcdefg' print(str1[0])# Output a because a is the first and its index is 0 print(str1[2])# Output c because c is the third and its index is 2 print(str1[6])# Output g because G is the seventh and its index is 6 print(str1[-1])# Output g. because G is the last one, its index can be represented by - 1, and others can also be obtained through negative values
String operation
String slicing
Slicing method: 'string' [start index: end index: step size]
Note: slice is a left closed right open interval. We learned the concept of left closed right open interval in high school,
That is, the value on the left can be obtained, and the value on the right can only be obtained by one digit lower than him
Slice basic operation
str1 = 'abcdefg'# Define a string variable with the content of 'abcdefg' print(str1)# Output abcdefg print(str1[0:2])# Output AB, the subscript of a is 0, and the subscript of b is 1. Since it is a left closed right open interval, 2 cannot be obtained print(str1[0:4:2])# Output ac # [0:4:2] explanation: the value from 0 to 3 is taken in the way of step size of 2, so only subscripts 0 and 2 are taken. The value corresponding to 0 subscript is a, and the value corresponding to 2 is c, so ac is output
Slicing advanced operations
str1 = 'abcdefg'# Define a string variable with the content of 'abcdefg' print(str1[:])# Output abcdefg default output 0 to the end print(str1[::])# Output abcdefg default output 0 to the end, step 1 print(str1[::-1])# Output gfedcba defaults to 0 to the end, but the step size is - 1, so the order is reversed print(str1[::-2])# Output geca in reverse order, end to 0, step 2 print(str1[2:-1])# Output cdef does not output g because of the left closed right open interval print(str1[2::])# The output cdefg does not need much explanation. This is for comparison with the following print(str1[2::-1])# The output of cba is in reverse order because of - 1, and then 2:: means to get 0 from 2, so cba is output
String splicing
+Plus sign splicing
str1 = 'character string' str2 = 'Splicing' print(str1+str2)# Output string splicing
f-string splicing
Format: f '
Explanation: will resolve variables in {}
str1 = 'character string' str2 = 'Splicing' str_f = f'Splicing:{str1}{str2}' print(str_f)# Output splicing: String splicing explanation: because str1 and str2 in {} are parsed, the string 'splicing:' is output as is
Duplicate string*
str1 = 'Duplicate string,' print(str1*3)# The output string is repeated, the string is repeated, and the character string is repeated,
Convert whole string to uppercase ()
str1 = 'abc' print(str1.upper())# Output ABC
Convert whole string to lowercase ()
str1 = 'ABC' print(str1.lower())# Output abc
Capitalize each word (title)
str1 = 'aa bb cc' print(str1.title())# Output Aa Bb Cc
String initial capital ()
str1 = 'aa bb cc' print(str1.capitalize())# Output Aa bb cc
Remove spaces at both ends (strip)
str1 = ' a b c ' # strip removes spaces at both ends (the middle space cannot be removed, and replace can be used to remove all spaces) print(str1.strip())# Output 'a b c' # lstrip removes the space on the left and l is the initial letter of left print(str1.lstrip())# Output 'a b c' # rstrip removes the space on the right and r is the initial of right print(str1.rstrip())# Output 'a b c'
Replace content
Format: 'string' replace('old content ',' new content ',' replacement times')
str1 = ' a b c ' print(str1.replace(' ', ''))# Output 'abc' print(str1.replace(' ', '', 2))# Output 'ab c' print(str1.replace('a', 'A'))# Output 'A b c'
Does the string contain only the number isnumeric()
Returns True if the string contains only numeric characters, otherwise False.
str1 = 'string' print(str1.isnumeric())# Output False str1 = '123' print(str1.isnumeric())# Output True str1 = 'string123' print(str1.isnumeric())# Output False
Specifies the number of occurrences of the content in the string isnumeric()
str1 = 'hello'.count('o')# Output 1 print(str1) str1 = 'hello'.count('oo')# Output 0 print(str1)
Specifies whether the content is included in the string find()
If it is the index value that returns the beginning, otherwise - 1 is returned
s = 'string' idx = s.find('t') print(idx)# Output 1 s = 'string' idx = s.find('tt') print(idx)# Output - 1
Specifies whether the content is included in the string index()
If it is the index value returned from the beginning, otherwise an error will be reported
s = 'string' idx = s.index('t') print(idx)# Output 1 s = 'string' idx = s.index('tt')# report errors
Whether the string starts with the specified content startswitch()
Yes, return True, otherwise return False
s = 'python' print(s.startswith('p'))# Output True
Whether the string ends with the specified content endswitch()
Yes, return True, otherwise return False
s = 'python' print(s.endswith('on'))# Output True
Zero zfill() is filled in front of the string
Returns a string of the specified length. The original string is right aligned. If the length is insufficient, 0 is filled in front of it
s = '11'.zfill(5) print(s)# Output 00011 s = '-11'.zfill(5) print(s)# Output-0011 s = '+11'.zfill(5) print(s)# Output + 0011 s = '11'.zfill(2) print(s)# Output 11 (because the fill length is 2)