Processing Text
This section discusses the string processing functions in Python.
Python text processing
Python text processing can be used:
Searching strings for specific information
Validating patterns of characters, digits, etc.
Transforming content between different formats
Manipulating and extracting string content
Preparing text for display to the user
Basic string operations
Import string module:
The string module contains some common string operations, such as some predefined string constants:
In Visual Studio Code, you can use Ctrl + Shift + P
. Open Command Palette, enter Python
, then select Run Python File in Terminal
, you can run Python code in Terminal.
If you print the above string, you can see their value:
Extract ASCII letters in strings:
The output result is:
This takes all the ASCII letters, and other numbers and spaces are excluded.
Check if only letters and numbers are included in the string, whether only letters are included, whether only numbers are included, respectively:
The output result is a BOOL value, for example, if the first Result is true, the character string contains only letters and numbers, if false, indicating characters other than the letters and numbers.
You can also use the all
function to check if you only contain letters and numbers in the string:
Searching strings
StartSwith
and Endswith
functions can be used to check whether the string starts or ends at the specified string, if yes, return true, otherwise returning false. For example:
The two outputs are: true and false.
find
and rfind
functions can be used to check if the string contains the specified string, if yes, return the index of the first match, otherwise returns -1. find
function looks from left to right, rfind
functions looks from right to left. For example:
The output results are: 31 and 31.
Check if you contain a specified string in the string:
The output result is: True.
Replace
function can be used to replace the specified string in the string, for example:
The output result is: The quick brown fox jumps over a lazy dog
。
The count
function can be used to count the appearance of the specified string in the string, for example:
The output result is: 1.
String manipulation
upper
and lower
functions can be used to convert uppercase letters in the string into lowercase letters, for example:
The output result is: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
and the quick brown fox jumps over the lazy dog
.
split
and join
can split and merge a string. The default separator of split
s is space. The string before join
is used to specify the separator. For example:
The output result is: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
and The quick brown fox jumps over the lazy dog
。
ljust
, rjust
and center
function can be used to specify the left, right or center alignment of the string, for example:
The output result is:
We can use the Translation Table to replace characters in the string, for example:
The output result is: Th2 q53ck br4wn f4x j5mps 4v2r th2 l1zy d4g
。
String formatting
We can use the template string to format the string, firstly import the template class of the String module:
Use the Template class to create a template, for example:
Then use the substitute
method to replace the variables in the template, for example:
The output result is: world, glorious world!
。
You can also create a Dictionary to replace variables in the template, for example:
The output result is: world, glorious world!
, the same as the above output.
The format
method can also be used to format strings, for example:
The output result is: The foo number is 123
, The 123 number is foo
, The foo number is 123
and The 7b number is 7B
. The last one is to convert the numbers in the string into hexadecimal form.
String interpolation
This feature requires at least Python 3.6 version. Import the datetime
module (later needs to format the time variable):
Format strings using f-String, for example:
The output result is: Today's date is 2021-11-17 and the product is coffee and the price is 4.99 with tax 0.35
.
More strings-related operations can be referred to Python Document.
Last updated