Python commonly used strings, string formatting expressions

one:

String methods commonly used in python3: method

Method call syntax:

object. method name (method parameter)

Description: A method call is an expression like a function call.

Common String Methods

method                                       illustrate

S.isdigit() determines whether the characters in the string are all digits

>>> a = '12345'
>>> a.isdigit()
True
>>>

S.isalpha() determines whether the string is full of English letters

>>> a = 'abcde'
>>> a.isalpha()
True
>>>

S.islower() Determines whether all characters in the string are all lowercase English letters

>>> a = 'abcd'
>>> a.islower()
True #all lowercase, return True
>>> a = 'abcdEF'
>>> a.islower() #There are uppercases, so return False
False
>>>

S.isupper() Determines whether all characters in the string are all uppercase English letters

>>> a = 'ABCD'
>>> a.isupper()
True
>>> a = 'ABCDcd'
>>> a.isupper() 
False
>>>

S.isspace() determines whether the string is all blank characters

>>> a = ' '    
>>> a.isspace()
True
>>> a = '1'    
>>> a.isspace()
False
>>>

S.center(width[,fill]) Center the original string and fill the left and right with spaces by default

>>> a = 'abcd'  
>>> a.center(20) #Default padding with spaces on both sides
'        abcd        '
>>> a.center(20,'#') #Defines the filling '#' filling on both sides #
'########abcd########'
>>> a.center(20,'+')
'++++++++abcd++++++++'
>>>

S.count(sub[, start[,end]]) Get the number of substrings in a string (commonly used)

>>> s = 'hello world'
>>> s.count('h') #Get how many h are in the string
1
>>> s.count('l')
3
>>> s.count(' ') #Get the number of spaces in the string
1
>>>

S.find(sub[, start[,end]]) Get the index of the substring sub in the string, return -1 on failure

parameter

  • sub -- specifies the substring to retrieve

  • S -- parent string

  • start -- optional parameter, start index, default is 0. (can be specified individually)

  • end -- optional parameter, the end index, defaults to the length of the string. (cannot be specified individually)

>>> x = 'abcde' #Start from subscript 0, find the substring that appears in the first substring of the string, because the weight is not defined to start the search, the default is to start with 0, and the return result is 0
>>> x.find('a')
0
>>> x.find('a',1) #Define the first position to start to find the first position of a in the string, because a is in position 0, and there is no other a after 0, so start from 1 If the bit starts to look for it, it cannot be found, so it displays -1
-1
>>> x.find('a',2)
-1
>>> x = 'abcdea' #This string has an extra a at the end. If you start from the second, there is a in the fifth place, so return 5, and you can find it in the fifth place.
>>> x.find('a',2)
5
>>>

S.strip() returns a string with left and right whitespace removed

>>> a = '  abc   ' 
>>> a
'  abc   '
>>> a.strip()
'abc'
>>>

S.lstrip() returns a string stripped of left whitespace characters

>>> a = '  abc   '
>>> a.lstrip()
'abc   '

S.rstrip() returns a string with whitespace removed from the right

>>> a = '  abc   '
>>> a.rstrip()
'  abc'

S.upper() produces a string that converts English to uppercase

>>> a = 'abcde'
>>> a.upper()  
'ABCDE'
>>>

S.lower() generates a string that converts English to lowercase

>>> a = 'ABCDE'
>>> a.lower()
'abcde'
>>>

The S.replace(old, new[, count]) method is used to replace the specified old substring in the string with the specified new substring. If the count optional parameter is specified, the specified number of times will be replaced, and all are replaced by default.

parameter:

  • old -- the specified old substring

  • new -- the specified new substring

  • count -- optional parameter, the number of times to replace, defaults to the total number of occurrences of the specified old substring in the string.

>>> a = 'abcdrf'       
>>> a.replace('a', 'h') #replace 'a' in the original string with the new character 'h'
'hbcdrf'
>>> a = 'abcdrfaca'    
>>> a.replace('a', 'g',2) #The character 'a' appears multiple times in the string. The latter 2 means to replace the previous two 'a's with 'g' from left to right
'gbcdrfgca'
>>> a.replace('a', 'g',1)
'gbcdrfaca'
>>> a.replace('a', 'g',3)
'gbcdrfgcg'
>>> a.replace('a', 'g') #If no cunt parameter is added after it, all are replaced by default.
'gbcdrfgcg'
>>>

S.startswith(prefix[, start[, end]]) Return whether S starts with prefix, if it starts with prefix, return True, otherwise return False,

parameter:

  • S -- parent string.

  • prefix -- specifies the prefix, this parameter can be a string or an element.

  • start -- optional parameter, the starting position index in the string, default is 0. (can be specified individually)

  • end -- optional parameter, the index of the end position in the character, the default is the length of the string. (cannot be specified individually)

>>> a = 'hello python'
>>> a.startswith('h')            
True
>>> a.startswith('p',6)
True
>>> a.startswith('o',4,1)
False
>>> a.startswith('o',4,4)
False

The S.endswith(suffix[, start[, end]])        method is used to determine whether the string ends with the specified suffix, if so, it returns True, otherwise it returns False

The parameters are the same as above

>>> a = 'hello python'
>>> a.endswith('n',11)
True
>>>

The following are not commonly used   

S.title() generates the capitalized string of the first letter of each English word

>>> a = 'hello python'   
>>> a.title()
'Hello Python'
>>>

S.isnumeric() determines whether the string is all numeric characters

>>> a = '1234'        
>>> a.isnumeric()
True
>>


two:

String formatting expression:

effect:

Generate a string in a certain format

operator:

%

Format:

format string % parameter value

or

format string % (parameter value 1, parameter value 2, ...)

Placeholders and Typecodes in Format Strings

placeholder type codesignificance

Python string formatting symbols:

    symbol describe
      %c  Formatting characters and their ASCII codes
      %s  format string
      %d  format integer
      % u  format unsigned int
      %O  format unsigned octal number
      %x  format unsigned hexadecimal number
      %X  format unsigned hexadecimal number (uppercase)
      %f  Format a floating point number, specifying the precision after the decimal point
      %e  Format floating point numbers in scientific notation
      %E  Same as %e, format floating point numbers in scientific notation
      %g  Shorthand for %f and %e
      %G  Shorthand for %f and %E
      %p  Format the address of a variable with a hexadecimal number


Example:

>>> n = "Name: %s, Age: %d"
>>> name= "张三"  
>>> age = 20
>>> print(n %(name, age))
Name: Zhang San, Age: 20
>>>

Format syntax between placeholder % and type code:

% [Format syntax] Type code (d, s, f, etc.)

Format syntax:

- left justified

+ show sign

0 padding

width

width.precision

>>> a = 'abc'
>>> print("%20s" %a) #align right, fill with spaces by default
                 abc
>>> 
>>> a = 'abc'
>>> print("%-10s" %a) #Left-aligned, padding with spaces by default
abc       
>>> 
>>> a = 3.1415926
>>> print("%.2f" %(a)) #Floating point decimal form, ".2" represents how many decimal places to take
3.14
>>> print("%.3f" %(a)) 
3.142
>>> print("%10.3f" %(a)) #浮点十进制形式,前面补十位空格
     3.142
>>> 
.......









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324658018&siteId=291194637