Common built-in functions of strings in Python

Posted by prueba123a on Thu, 12 Mar 2020 12:24:15 +0100

1. What is string built-in function

As long as a string is created, built-in functions (some functions prepared by the system) can be called by default.

2. Case related

The main functions are capitalization(), title(), upper(), lower(), etc.

  1. capitalize(): capitalize the first letter of the string

    message = 'you are a beautiful girl!'
    msg = message.capitalize()
    print(msg)
    #The output is: 'You are a beautiful girl!'
    
  2. title(): capitalize each word

    message = 'you are a beautiful girl!'
    msg = message.title()
    print(msg)
    #The output is: 'You Are A Beautiful Girl!'
    
  3. istitle(): determines whether the initial of each word is uppercase, and the return value is boolean type

    message = 'you are a beautiful girl!'
    msg = message.istitle()
    print(msg)
    #Output: False
    
  4. upper(): converts all strings to uppercase

    message = 'you are a beautiful girl!'
    msg = message.upper()
    print(msg)
    #The output is: 'YOU ARE A BEAUTIFUL GIRL'
    
  5. lower(): converts all uppercase letters of a string to lowercase

    message = 'You are a beautiful Girl!'
    msg = message.upper()
    print(msg)
    #The output is: 'you are a beautiful girl!'
    

3. Find and replace

It mainly includes find(), rfind(), lfind(), index(), rindex(), lindex(), replace(), etc.

  1. find(): returns the position of the character (string). If it is not found, the return value is - 1. If it can be found, the position where the letter (string) first appears will be returned.
    Function format: find('character to find ', start, end), start is to specify the starting position and end is to specify the ending position.

    message = 'you are a beautiful girl!'
    position = message.find('a')
    print(position)
    #Output: 4
    
  2. rfind() & lfind()

  • rfind(): indicates that retrieval starts on the right side
  • lfind(): indicates that retrieval starts on the left side
  • lfind() and rfind() are the same as find()
  1. index() & rindex() & lindex()
    Index(), rindex(), and lindex() are basically the same as the corresponding find(), rfind(), and lfind() functions, except for functions such as index(). If the str searched is not in the string, an exception will be reported, and find() functions will return - 1.

  2. Example: print file name

    url = 'https://www.baidu.com/img/bg_1.jpg'
    p = url.rfind('/')
    filename = url[p+1:]
    print(filename)
    
  3. replace(): the function prototype is replace(old,new,[max]), where [max] means optional and max means at most several replacements. Old is the string to be replaced, and new is the string to be replaced.

4. Encoding and decoding

Encoding and decoding are embodied in network transmission. Chinese characters usually involve encoding. The encoding format is the international standard of utf-8 by default in Python 3.

#Code
msg = 'It's nice of you to laugh.'
result = msg.encode('utf-8')

#Decode
m = result.decode('utf-8')
print(m)

5. Judge beginning and end

The functions are startswitch(), endswitch(), and the return values are all Boolean types.

  1. Startswitch(): judgment starts with xxx
  2. Endswitch(): judge whether to end with xxx
# Application: file upload
filename = 'note.docx'
# Determine if filename ends with docx
result = filename.endswith('docx')
print(result)

6. Judge whether it is a number

The main functions are: isalpha(), isdigit(), and the returned result is a Boolean value.

  1. isalpha(): judge whether it is all letters

    s = 'efgc7'
    result  = s.isalpha()
    print('result=',result)
    #False result
    
  2. isdigit(): judge whether all are numbers

    s = '7799'
    result = s.isdigit()
    print(result)
    #Result is True
    

7,join,split,count

  1. join() function

    new_str = '-'.join('abc')
    print(new_str)	
    #The output is' a-b-c '
    
    list = ['J','a','s','o','n']
    result = ''.join(list) 
    print(result)
    #The output is' Jason '
    
  2. split() function

    • lstrip()

      s = '  hi  '
      s = s.lstrip() #Remove space to the left of string
      print(s+'8')
      #Output result is' hi 8 '		
      
    • rstrip()

      s = 'hi  '
      s = s.rstrip() #Remove space on right
      print(s)
      #Output result is' hi '			
      
    • strip()
      Executing this function is equivalent to executing both lstrip() and rstrip() functions.

  3. count() function
    Find the number of args specified in the string.

    s = 'Your name is Jason'
    n = s.count('a')
    print(n)
    #Output: 2
    
42 original articles published, 24 praised, 3112 visited
Private letter follow

Topics: encoding network Python