P56 string built-in method

2019-09-19

07:46:48

'' ' 
String built-in method
* 1. counting the number of elements COUNT
2. capitloize initials
3. center centered
4. endswith determines whether the end of a content
if a content to 5. starswith Analyzing beginning
6. expandtabs increased space with the / t and (= 20 is TabSize)
7. the Find finds the first element, and the index value returned
8. rfind finds the last element and the index returns
another method 9. format formatted output {}
10. another method (dictionary) format_map formatted output
11. index search, and find the same, being given
whether 12. isalnum letters and digits
13. isdecimal determines whether the character string to be added in front of the decimal the U-
14. a isdigit and isnumeric as determined whether the digital
determination isidentfier 15. A
16. A islower determines whether lowercase
17. isupper determines whether uppercase
whether 18. isspace Analyzing only whitespace
19. istitle whether capitalized
20. lower replace all lowercase
21. upper replace all uppercase
'' '


#. 1. COUNT () method for statistical the number of times a character appears in a string. Optional parameters at the beginning and end of the string to search.
Str = "the this IS String Example .... WOW !!!"
Print (str.count ( "s")) # statistics "s" appears in str times. 3 >>>

# 2. capitalize () of the first letter capitalized string, other letters to lower case.
str02 = "string the this Example from runoob .... iS WOW !!!"
Print ( "str02 .capitalize (): ", str02.capitalize ( )) # str02 the first letter capitalized >>> str02.capitalize (): This is string example from runoob .... wow !!!

# 3 center () Returns a string of the original center, and padded with spaces to the width of the length of the new string. Default fill a space character.
Str03 = 'OH My God' # str03.center (length, element B)
Print (str03.center (50, '*')) # string center, with both sides of the element B does not fill the element B ******************** empty OH My god ********************* >>>

# 4. endswith () method for determining whether the string ends with the specified suffix returns True if the end with the specified suffix, otherwise False. Optional parameters "start" and "end" for the start and end position of the search string. (Ending)
# startsWith () method used to check if a string is specified at the beginning of the substring, if it is True is returned, otherwise returns False. If beg and end parameter specified value, it is checked in the specified range (starts)
str04 = "String the this Example .... IS WOW !!!"
suffix = "WOW !!!"
Print (str.endswith (suffix) )
Print (str.endswith (suffix, 20)) is designated # 20 i.e. the position of the element 20 is reached str04
suffix = "iS"
Print (str.endswith (suffix, 2,


# 5. Expandtabs () method of the tab character string ( '\ t') into a space, tab character ( '\ t') is 8. The default number of spaces (spaces only, with no eggs)
str05 = "the this iS \ tString Example .... WOW !!!";
Print ( "Original String:" + str05) # The default is to add eight >>> Original string: this is string example .... wow !! !
Print ( "defualt exapanded the Tab:" + str05.expandtabs ()) # without any change in
print ( "Double exapanded tab:" + str05.expandtabs (16)) # merely convert spaces can not add other elements >>> Double Tab exapanded: string the this Example .... iS WOW !!!

. #. 6 Find () method for detecting whether a string contains the substring str, if the specified BEG (start) and end (eND) range, it is checked whether comprising within a specified range, if the sub-string contains the index value of return, otherwise -1.
Like # index () and find () method, except that if not str string will report an exception. return -1 index fine given
str06 = "
print (str06.find ( "y") ) # Note that find only find the first element "A". 4 >>>
Print (str06.find ( "Z")) # If not found, returns -1 >> > -1
Print (str06.find ( "a",. 1)) # ". 1" refers str06 inside "a" position index. 1 >>>
Print (str06.find ( "a",. 3)) if # " a "str06 inside the" 3 "position is not str06, then -1 >>> returns - Note 1 str.find (" y ", position) if it returns 1 or -1 if not returns

# 7 str.. format (), which enhances the string formatting functions. The basic syntax {} and by:% instead of the previously formatted in another manner
# str.format_map () and format () is the same as but a variable format, a format of a dictionary
str07 = "I am is {name } "# braces only a niche content
print (str07.format (name ="


Print (stra7.format_map ({ 'name': "God"})) >>> # the I AM IS God

... 8 isalnum # () method for detecting whether a string of letters and numbers are determined isXXX statement, it returns True and Flash
str08 = "love abc0222" # Chinese can also
print (str08.isalnum ()) # returns True if it is a
Stra8 = "$$$ love"
Print (stra8.isalnum ()) # If it is not a return Flash

# 9. isdecimal () method checks whether the string contains only decimal characters. This method is only present in unicode object. Note: The definition of a decimal string, only you need to add 'u' to the prefix string before.
U = str09 "this2009"
Print (str.isdecimal ()) # not return False
value of the stra = U "23,443,434"
Print (str.isdecimal ()) # is the return True

# 10. Isdigit () method detects only whether the string of digits. If the string contains only digit returns True otherwise False. Float does not work, must be plastic
str10 = "225 313"
Print (str10.isdigit ()) # is the number of the True
stra10 = "abc1000"
Print (stra10.isdigit ()) is not a number # False

# 11. IsIdentifier () a method for judging whether the string is valid Python identifier, used to determine the variable name is legitimate.
print ( "China 123a" .isidentifier ()) # True >>>
print ( "" .isidentifier ()) Flash # >>>
print ( "_a" .isidentifier ()) # >>> True

# 12. islower ( ) the method of detecting whether a string of lower-case letters.
Whether # isupper () method for detecting a string of uppercase letters.
# Istitle () method to detect whether a string capitalized.
Str012 = "THIS IS String Example ...
Print (str012.islower ()) False >>> #

# 13 is. swapcase () method is used to convert lower case letters in the string. Primary variable uppercase to lowercase uppercase
str013 = "Abc"
Print (str013.swapcase ()) to abC >>> #

# 14. The Join () method is used to specify the elements of a sequence is connected to generate a new character string.
= str014 "-"
SEQ = [ "A", "B", "C"] # string sequence
Print (str014.join (SEQ)) >>> ABC #

. # 15 ljust () Returns a string of the original left aligned and padded with spaces to the width of the length of the new string. If the length is less than the length of the string is returned in the original string of characters left
# ljust () Returns a string of the original right-aligned and padded with spaces to the width of the length of the new string. If the length is less than the original length of the string is returned in the right character string
str15 = "Love"
Print (str15.ljust (15, "#"
Print (str15.rjust (15, "#")) >>> # ########### Love

# 16. Strip () method for removing head and tail of the specified character string (default spaces or line breaks) or a sequence of characters. Note: This method can only delete the beginning or end of the character, the character can not be deleted middle part. Line breaks can also remove
# lstrip () to remove the left spaces and line breaks
# rstrip () to remove the right of the spaces and line breaks
str16 = "ll \ n" # newline
print (str16.lstrip ()) # >>> ll removing spaces and line breaks

# 17. replace () method of the string old (old string) replaced with new (new string), if the third argument max, max is not more than the replacement times Alternatively str.replace (Old, new new [, max])
str17 = "MY Love"
Print (str17.replace ( "Love", "Bad")) #

Guess you like

Origin www.cnblogs.com/Black-sail/p/11546636.html