python string commonly used functions What?

Variable declaration
STR = "the Hello World"
Find () comprises detecting whether the string, the string returned position, If no return -1
str.find ( "the Hello") # Return Value: 0
str.find ( "W is") # return value: 6 note here under: space is also a character. Followed by a space in front of W, the W position. 6
str.find ( "R & lt") # Return values: -1, not included in the Hello World, does not include the return -1 if
index () detecting whether the character string contains the specified character, and returns the index started, if not contained will complain
str.index ( "Hello") # return value: 0
str.index ( "O") # return value: 4
str.index ( "W") # returns Found:. 6
str.index ( "R") # return value: error information, since R is not included. It is recommended that caution, there is no error if the value of the program is finished.
len () Returns the string length, in order to calculate the zero
len (str) # Return Value: 10
COUNT () to collect the number of characters specified in the string appears
str.count ( "o") Return value: 2, o characters There are two in Hello World.

You can also specify count () function from the start to find a location. The syntax is: count ( "", start, end)

str.count ( 'o', 5,10) Returns: 1 reason: after the specified start location will be retrieved from the index 5, the index 10 at the end. There is a 'o' between 5-10
str.count ( 'o', 4, len (str)) Return Value: 2, 4 from the index beginning to end of the string. len (str) string length
replace () replacement string
str.replace ( 'hello', 'HELLO ') # replace lowercase to uppercase hello HELLO
str.replace ( 'W', 'B') to W # replace B
Split () string cutting
str.split ( 'o') # returned as a list [ "hell", "w" , "rld"], hello world o is cut off inside the
upper () All the characters are converted to uppercase
str.upper () # returns a value of WORLD HELLO
title () converted to uppercase first letter
str.title () # return value: the Hello World
Center () returns a string of the original center, and a space to fill the width of the new string length
str.center (80) # return value: (Hello World) which is a space filled with two string
join () insert a specified character string in the back, to construct a new string
STR = " "
List = [ "I", "Love ", "You"

isspace () to detect whether a string contains only spaces, and vice versa if the return Trun returns False, the popular talk is non-empty judgment verify
str = ""
Strone = "Good morning!"
str.isspace () # returns Trun
strOne.isspace # returns to false
isalnum () detecting whether contain only numbers or letters. Uses: Analyzing the password can be used, in general not a password input Chinese characters or spaces
Strone = "A123"
strTwo = "A 456"
strOne.isalnum () Returns # Trun
strTwo.isalnum () # returns false, since the spaces
isdigit ( ) detects the character contains only numbers, and returns Trun False
STR = '123'
Strone = 'A123'
str.isdigit () returns Trun
str.isdigit () returns to false
the isalpha () detects whether a string contains only letters
str = "abcd "
Strone =" 123abacd "
str.isalpha () # returns Trun
strone.isalpha () # returns false
str1.
splitlines (keepend = False) Function: str1 rows slice, and the slice as a result of the returned list. keepend default is False,
when keepend to True when the display sections of the character [\ n]
str2.join (sequence)
Function: a character string in the sequence specified str2 spliced and spliced string.
min (str1)
Function: Returns the smallest character str1 [comparison] ASCII code value
max (str1)
Function: Returns the maximum character str1
str1.replace (old, new, count)
parameters a: is replaced by a character string
parameter II: the new string
of three parameters: the number of replacements, without specifying the default replace all
features: using the new replacement string str1 the old, if the count a specified count before the replacement,
if the specified count, then replace all.
Determining whether the string to begin with xx
str1.startswith ( "xx" [, start ] [, end])
If the beginning xx returns True, otherwise it returns False, if the specified range, the range [start, end),
if the specified range, the default is the entire string
to determine whether the string xx to the end
str1.endswith ( "xx")
if it ends with xx returns True, otherwise it returns False, if the specified range, the range [start, end ),
if the specified range, the entire string default
ordinary binary string to
str1.encode ()
binary string to string ordinary
str2.decode ()
Note: the format of the encoding and decoding formats must be consistent
mapping alternatives
 Generate a replacement table
dic = str4.maketrans ( "yn", "12")
be replaced according to the replacement mapping table
str1.isalpha ()
function: determines whether str1 pure letters, if True is returned, otherwise False.
Note: ZFX rebate www.fx61.com/brokerlist/zfx.html, this feature does not consider Chinese, Chinese is the default letter
str1.isalnum ()
function: to determine whether str1 by the numbers and letters, and if it returns True, otherwise return False.
Note: This feature does not consider Chinese, Chinese is the default letter
str1.isupper ()
function: to determine whether the letter str1 appears in all uppercase, if it returns True, otherwise return False
str1.islower ()
function: to determine str1 appears in whether all lowercase letters, and if it returns True, otherwise return False
str1.istitle ()
function: to determine whether a string str1 title, and if it returns True, otherwise return False
str1.isspace ()
function: to determine whether str1 contains only whitespace, if it returns True, otherwise False.
str1.isdigit (): only Arabic numerals identify
str1.isdecimal (): only Arabic numerals identify
str1.isnumeric (): In addition to Arabic numerals may also identify the Chinese one hundred twenty-three

Guess you like

Origin blog.51cto.com/14511863/2437801