current position:Home>Summary of common functions for processing strings in Python

Summary of common functions for processing strings in Python

2022-01-30 18:42:12 Lei Xuewei

Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities

This article has participated in  「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .

ceremonial Python Column No 23 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !

Today we talked about the basis of string , format , This time we'll talk about the common functions of strings , Don't miss !

The first two are in the same column of this article , Welcome to your attention . Let's start with .

What are the operations of strings ?

The actual development has these requirements :

The first category : Determine the recognition string

  • Determine which literal type the string belongs to ( Numbers , All letters , other )
  • The judgment string contains some structures ( Numbers are capitalized , Local substring , Occurrence frequency of substring, etc )

The second category : String editing operation ( Generate a new string )

  • String replacement / Merge / Filling, etc
  • Dictionary replacement , fill 0 value , Emptying operation, etc

The third category : Conversion of string and byte string .

This kind of operation usually occurs when data crosses programs / Cross server transport , We transmit bytes, Then get the transfer string type .

The first category Determine the recognition string

The school committee has prepared the following code :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13  In the morning 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : string_funs_cat1.py
# @Project : hello
import sys

slogan = "keep studying, keep coding, I am Levin"

#  Judging structure 
print(" Count how many times a substring appears : slogan.count('keep') = ", slogan.count('keep'))
print(" Find the subscript of the first occurrence of a substring : slogan.find('keep') = ", slogan.find('keep'))
print(" Find the subscript at the end of a substring : slogan.rfind('keep') = ", slogan.rfind('keep'))
print(" Find a substring subscript : slogan.index('keep') = ", slogan.index('keep'))
print(" Find a substring subscript : slogan.rindex('keep') = ", slogan.rindex('keep'))
print(" whether 'keep' Starting string : slogan.startswith('keep') = ", slogan.startswith('keep'))
print(" whether 'keep' Ending string : slogan.endswith('keep') = ", slogan.endswith('keep'))


#  String attribute related 
print(" String length : len(slogan) = ", len(slogan))
print(" Whether all strings are spaces : slogan.isspace() = ", slogan.isspace())
print(" Whether the string is capitalized : slogan.isupper() = ", slogan.isupper())
print(" Whether the string is lowercase : slogan.islower() = ", slogan.islower())
print(" Whether the string is capitalized for each word initial : slogan.istitle() = ", slogan.istitle())

#  Determine the string data type 
print(" Whether the string is all letters : slogan.isalpha() = ", slogan.isalpha())
print(" Whether the string is all numbers : slogan.isalnum() = ", slogan.isalnum())
print(" Whether the string is a number : slogan.isnumeric() = ", slogan.isnumeric())
print(" Whether the string is a floating point number : slogan.isdecimal() = ", slogan.isdecimal())
print(" Whether the string is a space string : slogan.isspace() = ", slogan.isspace())
 Copy code 

Readers can directly copy the running code , The school committee added the operation effect diagram :

 Screenshot  2021-10-31  Afternoon 6.15.58.png

The second category String editing operation

Next, the school committee has prepared some code displays :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13  In the morning 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : string_funs_cat2.py
# @Project : hello
import sys

slogan = "keep studying, keep coding, I am Levin"

print(" title case : slogan.capitalize() = ", slogan.capitalize())
print(" All letters are capitalized : slogan.upper() = ", slogan.upper())
print(" All letters are lowercase : slogan.lower() = ", slogan.lower())
print(" The first letter is capitalized ( Title style ): slogan.title() = ", slogan.title())
print(" Case reversal : slogan.swapcase() = ", slogan.swapcase())

table = slogan.maketrans({"e": "5"})
print(" String substitution table : slogan.translate(table) = ", slogan.translate(table))

#  String substitution , Merge , Filling, etc 
print(" Replace tabs by n A space : 'hello\t Academic Committee '.expandtabs(4) = '", "hello\t Academic Committee ".expandtabs(4))
print(" Left substring to concatenate the incoming list : ' '.join(slogan) = '", " ".join(slogan))
print(" Replace substring : ' '.replace(first, second) = '", slogan.replace("e", "11"))
print(" fill 0 value : slogan.rzfill(2)= '", slogan.zfill(50))
print(" fill # value : slogan.rjust(50,"#")= '", slogan.rjust(50,"#"))
print(" fill # value : slogan.ljust(50,"#")= '", slogan.ljust(50,"#"))
print(" Remove leading and trailing spaces : slogan.strip()= '", slogan.strip())

data = slogan.split("e")
print("split slogan into data= ", data)
 Copy code 

The operation effect is as follows :

 Screenshot  2021-10-31  Afternoon 6.17.55.png

The third category : Conversion of string and byte string

Next, the school committee has prepared some code displays :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/30 10:13  In the morning 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : string_funs_cat3.py
# @Project : hello
import sys

slogan = "keep studying, keep coding, I am Levin"
bytes = slogan.encode("utf-8")
print("type of encoded string = ", type(bytes))
#  Be careful python Of string There is no type decode function , This function belongs to bytes Type object specific !!!
print("type of decoded byte = ", type(bytes.decode("utf-8")))
print("type of decoded byte = ", bytes.decode("utf-8"))
 Copy code 

The operation effect is as follows :

 Screenshot  2021-10-31  Afternoon 6.20.16.png

summary

In fact, the school committee also missed several functions , But I don't want to introduce them .

Learning to program is not about memorizing , But not everything depends on feeling .

I like the following sentence :

Reading breaks thousands of volumes , Writing like a God !

Well prepared code , Readers run , I feel !( The notes are very clear )

by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !

copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301842108498.html

Random recommended