Introduction to str internal functions
1,pitalize (self): capitalize the first letter of the string
s = ' Hello '
result = S.capitalize ()
Print (Result) Result: Hello
2,casefold (self): see string becomes lowercase
s = ' HELLO '
result = S.casefold ()
Print (result) Result: Hello
3,center (self, Width, fillchar=none): Centered by string, padding by default with spaces
s = ' HELLO '
result = S.center (20, ' * ')
Print (Result) Result: *******hello********
4, COUNT (self, sub, Start=none, End=none): Counts the number of occurrences of a character in a string, and can be specified as the last position.
s = ' HELLO '
result = S.count (' L ')
Print (Result) results: 2
5, endswith (self, suffix, start=none, end=none): Determines whether a string has a character or end of a string, and can be specified as the last position.
s = ' HELLO '
result = S.endswith (' LO ')
RESULT1 = S.endswith (' LX ')
Print (Result) Result: True
Print (RESULT1) Result: False
6, Expandtabs (self, tabsize=8): Replaces the TAB key in a string with a space, the default 8 spaces
s = ' hello\tworld! '
result = S.expandtabs ()
RESULT1 = S.expandtabs (20)
Print (Result) Result: HELLO world!
Print (RESULT1) Result: HELLO world!
7, find (self, sub, Start=none, End=none): Finds a character or string in a string, returns a subscript if found, if not found-1
s = ' helloworldhello! '
result = S.find (' LO ')
RESULT1 = S.find (' LX ') print (Result) results: 3
Print (RESULT1) Results: 1
8, Index (self, sub, Start=none, End=none): Finds a character or string in a string, if it finds a return subscript, if no throw exception is found (error)
s = ' helloworldhello! '
result = S.index (' LO ')
Print (Result) results: 3
s = ' helloworldhello! '
RESULT1 = S.index (' LX ')
Print (RESULT1) The following results: Error content
Traceback (most recent): File "c:/python/test2.py", line 3, <module>
&NBSP;RESULT1 = S.index (' LX ')
Valueerror:substring not found
9, Format (self, *args, **kwargs): String formatting
s = ' Flowers '
Print (' This is {}! '). Format (s))
Print (' This is {0} flowers, which is {1} flowers '. Format (' Red ', ' yellow '))
Print (' Colour {name} and {name1} '. Format (name1 = ' yellow ', name = ' Red ')
Results:
This is flowers!
This is red flowers, which is yellow flowers
Colour red and yellow
Isalnum (self): Determine whether a string consists only of numbers and letters
s = ' flowers12 '
S1 = ' flowers12!! '
result = S.isalnum ()
RESULT1 = S1.isalnum ()
Print (Result) Result: True
Print (RESULT1) Result: False
One, Isalpha (self): Determines whether a string consists only of letters
s = ' Flowers '
S1 = ' FLOWERS12 '
result = S.isalpha ()
RESULT1 = S1.isalpha ()
Print (Result) Result: True
Print (RESULT1) Result: False
IsDigit (self): Determines whether a string is a number:
s = ' 1223 '
S1 = ' FLOWERS12 '
result = S.isdigit ()
RESULT1 = S1.isdigit ()
Print (Result) Result: True
Print (RESULT1) Result: False
Isdecimal (self): Determines whether a string is a decimal number
s = ' 1223 '
S1 = ' 0x1223 '
result = S.isdecimal ()
RESULT1 = S1.isdecimal ()
Print (Result) Result: True
Print (RESULT1) Result: False
Isidentifier (self): Determine if an identifier is valid
s = ' 12hello '
S1 = ' Hello '
result = S.isidentifier ()
RESULT1 = S1.isidentifier ()
Print (Result) Result: False
Print (RESULT1) Result: True
Islower (self): Determines whether a string is lowercase.
s = ' Hello '
S1 = ' Hello '
result = S.islower ()
RESULT1 = S1.islower ()
Print (Result) Result: False
Print (RESULT1) Result: True
Isidentifier (self): Determine if a string is numeric only
s = ' 1256 '
S1 = ' test1256 '
result = S.isnumeric ()
RESULT1 = S1.isnumeric ()
Print (Result) Result: True
Print (RESULT1) Result: False
17,isspace (self): Determines whether a string is a space
s = '
S1 = ' Test 1256 '
result = S.isspace ()
RESULT1 = S1.isspace ()
Print (Result) Result: True
Print (RESULT1) Result: False
18,istitle (self): Determines whether a string is a caption
s = ' Hello world '
S1 = ' Hello World '
result = S.istitle ()
RESULT1 = S1.istitle ()
Print (Result) Result: True
Print (RESULT1) Result: False
19,isupper (self): Determines whether a string is uppercase
s = ' HELLO '
S1 = ' Hello '
result = S.isupper ()
RESULT1 = S1.isupper ()
Print (Result) Result: True
Print (RESULT1) Result: False
20,join (self, iterable): Splits the argument (interable) with a string
s = ' HELLO '
result = S.join (' xxx** ')
Print (Result) Result: xhelloxhelloxhello*hello*
Ljust (self, width, fillchar=none) and rjust (self, width, fillchar=none) fills the specified width from left or right with the specified character
s = ' HELLO '
result = S.ljust (12, ' * ')
RESULT1 = S.rjust (12, ' * ')
Print (Result) Result: hello*******
Print (RESULT1) Result: *******hello
Lower (self) and upper: turns a string into lowercase and uppercase
s = ' HELLOworld '
result = S.lower ()
RESULT1 = S.upper ()
Print (Result) Result: HelloWorld
Print (RESULT1) Result: HELLOWORLD
Lstrip (self, Chars=none) and Rstrip (self, chars=none): Removes the specified character from the left or right
s = ' HELLOworld '
result = S.lstrip (' HE ')
RESULT1 = S.rstrip (' ld ')
Print (Result) Result: Lloworld
Print (RESULT1) Result: Hellowor
23,partition (Self, SEP): Splits a string into a tuple according to the specified character
s = ' HELLOworld '
result = s.partition (' wo ')
RESULT1 = s.partition (' ss ')
Print (Result) Result: (' HELLO ', ' wo ', ' rld ')
Print (RESULT1) Result: (' HELLOworld ', ' ', ')
Replace (self, old, new, Count=none): Replaces older characters with new characters, count specifies the number of overrides, default all overrides
s = ' Hello Word door '
result = S.replace (' o ', ' T ')
RESULT1 = S.replace (' o ', ' T ', 2)
Print (Result) Result: Hellt wtrd dttr
Print (RESULT1) Result: Hellt wtrd Door
25,split (self, Sep=none, maxsplit=-1): Divides a string with a pointing character into a list, and the specified character is automatically removed.
s = ' Helloworddoor '
result = S.split (' o ')
Print (Result) Result: [' hell ', ' w ', ' Rdd ', ' ', ' R ']
26,splitlines (self, keepends=none): Divides a string into a list by rows
s = "'
This is a cat
That's a dog
‘‘‘
result = S.splitlines ()
Print (Result) Result: [', ' This was a cat ', ' That's a dog ', ']
27,startswith (self, prefix, Start=none, End=none): Determines whether a string ends with a guided character.
s = ' HelloWorld '
result = S.startswith (' he ')
RESULT1 = S.startswith (' el ')
Print (Result) Result: True
Print (RESULT1) Result: False
Swapcase (self): Converts a string case to uppercase
s = ' HELLOworld '
result = S.swapcase ()
Print (Result) Result: HelloWORLD
29,title (self): Convert a string to a caption
s = ' This is a buautiful flowers '
result = S.title ()
Print (Result) Result: This is A buautiful Flowers
30,maketrans (self, *args, **kwargs) and Maketrans (self, *args, **kwargs): replace the corresponding character with the mapped situation, and the last parameter is to remove the character.
Python beginner day2--(String (str) internal features introduction)