Introduction to STR internal functions
Name = ' Eric ' name = Str (' Eric ')
See what types of variables belong to
Print (Type (name))
See which members are available for use
Print (dir (name))
In any language, whether in programming languages like Java,c++,c#,javascript or ruby, strings are important, so it is necessary to learn the strings well.
__contains__ ()
Whether a string contains another string
name = str (' Eric ')
RESULT1 = ' er ' in name
RESULT2 = name.__contains__ (' Er6 ')
Print (RESULT1)
>>>true
Print (RESULT2)
>>>false
Capitalize ()
Capitalize first letter
Name = ' Eric '
result = Name.capitalize ()
Print (Result)
>>>eric
Casefold ()
First Letter Lowercase
name = ' Eric '
Print (Name.casefold ())
>>>eric
Swapcase ()
Uppercase converted to lowercase, lowercase to uppercase
name = ' Eric '
result = Name.swapcase ()
Print (Result)
>>>eric
Center (PARAM1,PARAM2)
Center A string
PARAM1: Total Placeholder
PARAM2: Default is a space, you can customize
Name = ' Eric '
RESULT1 = Name.center (20)
RESULT2 = Name.center (20, ' * ')
Print (RESULT1)
>>> Eric
Print (RESULT2)
eric********
Count (Sub,start,end)
Sub: string
Start: Start position
End: Ending position
The number of occurrences of a character, or the number of occurrences of one substring
name = ' 12323222323242342 '
result = Name.count (' 2 ')
Print (Result)
>>>9
Encode ()
transcoding
name = ' Xiaoming '
result = Name.encode (' GBK ')
Print (Result)
>>>b ' \xd0\xa1\xc3\xf7 '
EndsWith (Suffix,start,end)
Suffix: Sub-string
Start: Starting position
End: Ending position
Determines whether a string ends with a character and a substring of the latter
name = ' Alex '
result = Name.endswith (' x ')
Print (Result)
>>>true
Expandtabs ()
Change tab to the default 8 spaces
name = ' A\tlex ' #\t: Indicates a space
result = Name.expandtabs ()
Print (Result)
Print (len (result))
>>>a Lex
>>>11 here because I counted \ t, so it was 11.
Find (Sub,start,end)
The position of a character or string
name = ' Alex '
result = Name.find (' Le ')
Print (Result)
>>>1
Note: Cannot find equals-1
Index ()
The position of a character or string
name = ' Alex '
result = Name.index (' Le ')
Print (Result)
Can not find the error
Format ()
Concatenation of strings in some format
(1)
Name = ' Alex {0} as {1} '
result = Name.format (' sb ', ' Eric ')
Print (Result)
>>>alex SB as Eric
(2)
Name = ' Alex {name} ' as {ID} '
result = Name.format (name= ' sb ', id= ' Eric ')
Print (Result)
>>>alex SB as Eric
Join ()
Used for stitching.
Li = [' A ', ' B ', ' C ', ' d ']
RESULT1 = "". Join (LI)
RESULT2 = "_". Join (LI)
Print (RESULT1)
>>>abcd
Print (RESULT2)
>>>a_b_c_d
Partition ()
Splitting strings as prompted
Name = "ALEXISSB"
result = Name.partition (' is ')
Print (Result)
>>> (' Alex ', ' is ', ' SB ')
Replace (Str,newstr,num)
Replace a character or substring
Name = "ALEXIDSB"
result = Name.replace ("SB", "BS")
Print (Result)
>>>alexidbs
Splitlines ()
Cutting according to the row
Name = "" "
Aa
Bb
CC "" "
result = Name.splitlines ()
Print (Result)
>>>[", ' AA ', ' BB ', ' cc ')
The internal function of the string is many, the above just introduces some common and relatively special internal functions, want to understand the string of other functions can query its source code.
Introduction to Python's internal function of STR