Python string full solution

1. String case conversion

1  def strChange():
 2      str = " [email protected] " 
3      print ( " Original string: " + str)
 4      print ( " Convert letters to lowercase: " + str.lower())
 5      print ( " Convert letters to uppercase: " + str.upper())
 6      print ( " Convert uppercase to lowercase, lowercase to uppercase: " + str.swapcase())
 7      print ( " Capitalize first letter: " + str.title( ))

2. String test

1  def strTest():
 2      str = " [email protected] " 
3      print ( " Original string: " + str)
 4      print ( " Are all letters: s.isalpha=%s " % str.isalpha() )
 5      print ( " Whether it's all digits: s.isdigit=%s " % str.isdigit())
 6      print ( " Whether it's all whitespace characters: s.isdigit=%s " % str.isspace())
 7      print ( " Whether the letters are all lowercase: s.islower=%s "% str.islower())
 8      print ( " Whether the letters are all uppercase: s.isupper=%s " % str.isupper())
 9      str = " Niuxinlong "   #Note that "[email protected]" is If it cannot be detected, the letters in the string cannot contain other characters (non-letters) 
10      print ( " Whether the first letter is capitalized: s.istitle=%s " % str.istitle())

3. String segmentation and combination

1  def strSplit():
 2      str = " I love JuJingyi! " 
3      print (str.split()) #The   default separator is a blank character, and it is split into a list form 
4      print (str.split( " i " , 2))   #Use the character "i" as the delimiter, and divide it twice. If the number of divisions exceeds the maximum number of divisions of the string, the maximum number of divisions is 
5      strList = [ " hello " , " Word " , " ! " ]
 6      print ( "" .join(str))   #Separator .
     "  " .join(strList))
 8      print ( " # " .join(strList))
 9      str = " Hello! " 
10      print ( "  " .join(str))   #If the parameter of the join() method is a string, The default delimiter joins each character

4. String Search and Replace

1  def strFind():
 2      str = " I love ju Jingyi! " 
3      print (str.find( " i " ))   #Print the label of the first "i" returned, otherwise return -1 
4      print (str. count( " i " ))   #Count the number of times the character "i" appears in the string 
5      print (str.replace( " j " , " J " , 1))   #Replace the lowercase "j" in the string for uppercase "J" 
6      print (str.  lstrip()) #Remove the spaces on the left side of the string 
7      print (str.rstrip())  #Remove the spaces on the right side of the string 
8      print (str.strip()) #Remove   the spaces on the left and right sides of the string

 

if __name__ == "__main__":
  print( "--------- String case conversion ---------")
  strChange()
  print( " \n ------ --- String test ---------")
  strTest()
  print( " \n --------- String splitting and combining ---------")
  strSplit()
  print( " \n --------- String search and replace ---------")
  strFind()

Associated Blog (CSDN): https://blog.csdn.net/m0_38022608/article/details/80207546

 

 
 

Guess you like

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