String type (String)
A string is ‘ any text enclosed in single or double quotation marks " , such as ‘abc‘ , and "123" so on.
Note that ‘‘ or "" itself is just a representation, not part of a string, so the string is ‘abc‘ only a , b c this 3 characters. If it ‘ is also a character, it can be "" enclosed, such as the character that contains it,,, the space, the "I‘m OK" I ‘ m O K 6 characters.
1. Create a string
b = ' ASDASD '
2. String manipulation
A. Repeating output string
Print (' Python ' * 5)
>>>pythonpythonpythonpythonpython
B. Get the characters in the string by index, here and the slice operation of the list is the same, the concrete content see list
Print (' Hello World ' [2:])
>>>llo World
C.in member Operator-returns TRUE if the string contains the given character
Print (' RL ' in ' Hello World ')
>>>true
d.% format string
Print ("She sis a Good Girl") print ("She sis a%s girl"% ' good ')
>>>she is a good girl
>>>she is a good girl
E. String concatenation a
A = ' zxc ' b = ' asd ' c = ' qwe ' Print (a+b+c)
>>>zxcasdqwe
The above method is very inefficient, it is recommended to do string concatenation with join
A = ' zxc ' b = ' asd ' c = ' qwe ' Print ('. Join ([a,b,c])) print (' + '. Join ([a,b,c]))
>>>zxcasdqwe
>>>zxc+asd+qwe
3. String built-in method
Capitalize capitalizes the first character of a string and does not modify the original string, returning a new string
A = ' Hello world! ' Print (A.capitalize ())
>>>hello world!
Center returns an original string centered and uses * to fill a new string of length 20
A = ' ASD ' Print (A.center (20, ' * '))
>>>********asd*********
Number of elements in count statistics string
A = ' Hello world! ' Print (A.count (' l '))
>>>3
EndsWith determines whether to return a Boolean value at the end of a content
A = ' Hello world! ' Print (A.endswith (' ld! '))
>>>true
StartsWith determines whether to start with a certain content, returns a Boolean value
Expandtabs (tabsize=8) Turns the tab symbol in the string to a space, and the tab default number of spaces is 8.
A = ' hello\t worl\td! ' Print (A.expandtabs (tabsize=10))
>>>hello Worl d!
String.find (str, beg=0, End=len (string)) detects if STR is contained in a string, and if beg and end specify a range, the check is contained within the specified range, or 1 if the index value of the start is returned.
A = ' Hello world! ' Print (A.find (' or ')) print (A.find (' or ', 5,9)) print (A.find (' or ', 2,5))
>>>7
>>>7
>>>-1
String.index (str, beg=0, End=len (String)) is the same as the Find () method, except that STR does not report an exception if it is not in a string.
A = ' Hello world! ' Print (A.index (' or ')) print (A.index (' or ', 5,9)) print (A.index (' or ', 1, 3))
>>>7
>>>7
>>>traceback (most recent):
File "str.py", line 6, <module>
Print (A.index (' or ', 1, 3))
>>>valueerror:substring not found
String.isalnum () returns True if all characters of string are letters or numbers, otherwise False
A = ' HelloWorld ' b = ' 123123 ' c = ' python3 ' d = ' python 3 ' Print (A.isalnum ()) print (B.isalnum ()) print (C.isalnum ()) Print ( D.isalnum ())
>>>true
>>>true
>>>true
>>>false
String.isalpha () returns True if all characters of string are alphabetic, otherwise False
A = ' HelloWorld ' c = ' python3 ' Print (A.isalpha ()) print (C.isalpha ())
>>>true
>>>false
String.isdecimal () returns True if the string contains only a decimal number, otherwise False is returned.
String.isdigit () returns True if the string contains only a number, otherwise False.
String.islower () returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are lowercase, otherwise False
String.isnumeric () returns True if the string contains only numeric characters, otherwise returns Falsetrue:unicode number, byte digit (single byte), full-width number (DWORD), Roman numeral false: Chinese digit error: No true:unicode numbers, full-width digits (double byte) false: Roman numerals, kanji digits error:byte digits (single byte) true:unicode digits, full-width digits (double-byte), Roman numerals, Chinese numerals false: no error: A byte numeric (single-byte) string.
String.istitle () returns True if string is heading (see Title ()), otherwise False
A = ' Hello world ' b = ' Hello World ' Print (A.istitle ()) print (B.istitle ())
>>>false
>>>true
String.isupper () returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are uppercase, otherwise False
String.Join (seq) merges all the elements in the SEQ (the string representation) into a new string, using string as a delimiter
= B = (. Join ()) (. Join ([b]))
>>>m+y+ +g+i+r+l
>>>my girl$xxxx
String.ljust (width) returns the left alignment of an original string and fills the new string with a space of length width
= (. Ljust ())
>>>my GIRL-------------
String.Lower () converts all uppercase characters in a string to lowercase.
String.lstrip () truncates the left space of a string
String.maketrans (Intab, Outtab]) is used to create a conversion table for character mappings, for the simplest invocation of two parameters, the first argument is a string, which represents the character that needs to be converted, and the second argument is the target of the string representation transformation.
= (. Maketrans ())
>>>{120:98}
Max (str) returns the largest letter in the string str.
MIN (str) returns the smallest letter in the string str.
String.partition (str) is a bit like the combination of find () and split (), which separates string strings into a 3-element tuple from the first position in STR (string_pre_str,str,string _POST_STR), if the string does not contain str then string_pre_str = = String.
String.Replace (str1, str2, Num=string.count (STR1)) replaces str1 in string with STR2, and if NUM is specified, the substitution is not more than num times.
String.rfind (str, Beg=0,end=len (string)) is similar to the Find () function, but looks up from the right.
String.rindex (str, Beg=0,end=len (string)) is similar to index (), but starts from the right.
String.rjust (width) returns the right alignment of the original string and fills the new string with a space of length width
String.rpartition (str) is similar to the partition () function, but looks up from the right.
String.rstrip () Deletes a space at the end of a string string.
String.Split (str= "", Num=string.count (str)) slices a string with the Str delimiter, and if NUM has a specified value, only the NUM substring is delimited
# String.splitlines (num=string.count (' \ n ')) is separated by rows, returns a list containing the rows as elements, and if NUM specifies that only num rows are sliced.
# String.startswith (obj, Beg=0,end=len (string)) checks whether the string starts with obj, returns True, or False. If beg and end specify a value, the check is within the specified range.
# String.strip ([obj]) executes Lstrip () and Rstrip () on string
# string.swapcase () Flip case in string
# String.title () returns a "header" string, meaning all words start with uppercase and the remaining letters are lowercase (see istitle ())
# string.translate (str, del= "") converts string characters according to the table given by STR (contains 256 characters), and the characters to be filtered out into the del parameter
# string.upper () Convert lowercase letters in string to uppercase
Python Learning note string (ii)