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()
    #The output is: 'You are a beautiful girl!'
  2. title(): capitalize each word

    message = 'you are a beautiful girl!'
    msg = message.title()
    #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()
    #Output: False
  4. upper(): converts all strings to uppercase

    message = 'you are a beautiful girl!'
    msg = message.upper()
    #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()
    #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')
    #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 = ''
    p = url.rfind('/')
    filename = url[p+1:]
  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.

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

m = result.decode('utf-8')

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')

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()
    #False result
  2. isdigit(): judge whether all are numbers

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


  1. join() function

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

    • lstrip()

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

      s = 'hi  '
      s = s.rstrip() #Remove space on right
      #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')
    #Output: 2
42 original articles published, 24 praised, 3112 visited
Private letter follow

Topics: encoding network Python