Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The 23rd official Python column, stop! Don’t miss this zero-based article!

Today we talked about the basic string, formatting, this time we will explain the string of common functions, do not miss!

The first two are in the same column. So let’s start.

What operations do strings have?

Actual development has these requirements:

The first category: judgment recognition strings

  • Determine which literal type the string belongs to (number, full letter, other)
  • Determine if the string contains some structure (number capitalization, local substring, substring frequency, etc.)

Type 2: String editing operations (generating new strings)

  • String substitution/merge/fill, etc
  • Dictionary replacement, padding 0 values, empty operations, etc

The third type: string and byte string interchange.

This type of operation usually occurs when data is transferred across programs/servers. We transfer bytes and then fetch the string.

The first type determines the recognition string

The committee prepared the following code:

#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: Hello import sys slogan = "Keep studying, keep coding, I am Levin" Slogan. Count ('keep') = ", slogan. Count ('keep')) print(" Slogan. Find ('keep') = ", slogan. Find ('keep')) print(" Find the subscript that appears at the end of a substring: Slogan. Rfind ('keep') = ", slogan. Rfind ('keep')) print(" Slogan. Slogan. Index ('keep') = ", slogan. Index ('keep')) print(" Slogan. Rindex ('keep') = ", slogan. Rindex ('keep')) print(" If slogan. Slogan. Startswith ('keep') = ", slogan. Startswith ('keep')) print(" If 'keep' ends string: Slogan. Endswith ('keep') = ", slogan. Endswith ('keep')) # String attribute correlation print(" String length: Len (slogan) = ", len(slogan)) print(" If the string is empty: slogan.isspace() = ", slogan.isspace()) print(" If the string is capitalised: Slogan. Isupper () = ", slogan. Isupper ()) print(" String is lowercase: slogan. Islower () = ", slogan. Slogan. Istitle () = ", slogan. Istitle ()) # Slogan.isalpha() = ", slogan.isalpha()) print(" Is the string all numbers: slogan.isalnum() = ", slogan.isalNum ()) print(" Is the string number: Slogan. Isnumeric () = ", slogan. Isnumeric ()) print(" Is the string floating point: Slogan. Isdecimal () = ", slogan. Isdecimal ()) print(" Is the string a space string: slogan.Copy the code

Readers can directly copy the running code, the committee has added the running renderings:

The second type of string editing operation

Here are some code demos prepared by the committee:

#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: Hello import SYS slogan = "Keep studying, keep coding, I am Levin" print(" initial capitalization: Slogan. Capitalize () = "Slogan. Capitalize () print(" Capitalize: slogan. Upper () = ", slogan. Slogan. Lower () = ", slogan. Lower ()) print(" Convert to uppercase (title style) : slogan. Title () = ", slogan.  slogan.swapcase() = ", slogan.swapcase()) table = slogan.maketrans({"e": "4 "}) print(" slogan. Translate (table) = ", slogan. Translate (table)) 'Hello \t学 生 '. Expandtabs (4) = '", "Hello \ T 学 生 ". Expandtabs (4)) print(" ' '. Join (slogan) = '", "". Join (slogan)) print (" replace the substring: ' '. The replace (first, second) = '", the slogan. The replace (" e ", "11")) print (" fill 0 value: Slogan. Rzfill (2) = ', "slogan. The zfill (50)) print (" # fill values: slogan. Rjust (50," # ") = '", the slogan. Rjust (50, "#")) print (" filling # value: Slogan. Ljust (50, "#") = '", the slogan, ljust (50, "#")) print (" removing leading and trailing Spaces: slogan.strip()= '", slogan.strip()) data = slogan.split("e") print("split slogan into data= ", data)Copy the code

The running effect is as follows:

The third type: string and byte string interchange

Here are some code demos prepared by the committee:

#! /usr/bin/env python # -*- coding: utf-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: hello import sys slogan = "keep studying, keep coding, I am Levin" bytes = slogan.encode("utf-8") print("type of encoded string = ", Note that Python has no decode function for string types. This function is specific to bytes objects!! print("type of decoded byte = ", type(bytes.decode("utf-8"))) print("type of decoded byte = ", bytes.decode("utf-8"))Copy the code

The running effect is as follows:

conclusion

Actually, the committee left out a couple of functions, but I won’t go into them.

Learning to program isn’t about memorizing, but it’s not about feeling.

I like this quote:

Reading broken volumes, such as god!

Carefully prepared code, the reader run, their own perception! (The comments are very clear)

By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!