This article example describes the Python string built-in function function and usage. Shared for your reference, as follows:
String Built-in Summary
Something to keep in mind:
- String single quotes and double quotes can not cancel the meaning of special characters, if you want to make all the characters within the quotes are canceled special meaning, in front of the quotes add r, such as name = r 'l\thf'
- Unicode strings used in conjunction with r must be preceded by r, e.g. name = ur'l\thf'.
case sensitive
function (math.) | corresponds English -ity, -ism, -ization | typical example | exports |
---|---|---|---|
capitalize | capitalize the first letter and lowercase the rest | ‘lk with psr'.capitalize() | ‘Lk with psr' |
upper | all caps | ‘lk with psr'.upper() | ‘LK WITH PSR' |
lower | all lowercase (letters) | ‘lk with psr'.lower() | ‘lk with psr' |
swapcase() | case sensitive | ‘Lk with Psr'.swapcase() | ‘lK WITH pSR' |
.title() | uppercase letters | ‘lk with psr'.title() | ‘Lk With Psr' |
Determining the type of character in a string
function (math.) | corresponds English -ity, -ism, -ization | typical example | exports |
---|---|---|---|
startswith(‘a',[start,end]) | Does it start with a | ‘a lk'.startswith(‘a') | True |
endswith(‘a') | Does it end in a | ‘lk'.endswith(‘a') | False |
isalnum() | Whether all letters or numbers | ‘123asd'.isalnum() | True |
isalpha() | All letters or not | ‘lk12'.isalpha() | True |
isdigit() | All-digital or not | ‘123'.isdigit() | True |
islower() | Lowercase or not | ‘lk'.islower() | True |
isupper() | All-caps or not | ‘lk'.isupper() | False |
istitle() | Determine if an initial letter is uppercase | ‘Lk'.istitle() | True |
isspace() | Determine if a character is a space | ' '.isspace() | True |
String Replacement
function (math.) | corresponds English -ity, -ism, -ization | typical example | exports |
---|---|---|---|
replace(‘old',‘new') | Replace old with new | 'hello world'.replace(‘world',‘python') | hello python |
replace('old','new', times) | Replace old with new for the specified number of times | 'hello world'.replace(‘l',‘p',2) | heppo world |
remove spaces
function (math.) | corresponds English -ity, -ism, -ization | typical example | exports |
---|---|---|---|
strip() | Remove the spaces on both sides. | ' h e-l lo '.strip() | conceivable |
lstrip() | Remove the space to the left. | ' h e-l lo '.lstrip() | conceivable |
rstrip() | Remove the space to the right. | ' h e-l lo '.rstrip() | conceivable |
Concatenate individual characters with specific characters
function (math.) | corresponds English -ity, -ism, -ization | typical example | exports |
---|---|---|---|
.join() | -Connections | ‘-'.join([‘a', ‘b', ‘c']) | a-b-c |
Splitting a string with a specific character in the string
function (math.) | corresponds English -ity, -ism, -ization | typical example | exports |
---|---|---|---|
split() | Separated by spaces by default | ' h e-l lo ' .split() | [‘h', ‘e-l', ‘lo'] |
split('specified character') | Split strings into arrays by specified characters | ' h e-l lo ' .split('-') | [' h e', 'l lo '] |
look for sth.
function (math.) | corresponds English -ity, -ism, -ization | typical example | exports |
---|---|---|---|
find() | Search for the specified string, no return -1 | ‘lk la'.find(‘lk') | 0 |
index() | Ditto, but I get an error if I can't find it. | ‘lk la'.index(‘lk') | 0 |
rfind() | Search from the right | ‘lk la'.rfind(‘lk') | 0 |
count() | Counts the number of occurrences of the specified string | ‘lklklk'.count(‘lk') | 3 |
The difference between the str functions isdigit, isdecimal, and isnumeric in python.
isdigit()
True: Unicode numbers, byte numbers (single-byte), full-width numbers (double-byte), Roman numerals
False: Kanji numbers
Error: None
isdecimal()
True: Unicode digits,, full-width digits (double-byte)
False: Roman numerals, Chinese numerals
Error:: byte numbers (single byte)
isnumeric()
True: Unicode numerals, full-width numerals (double-byte), Roman numerals, Kanji numerals
False: None
Error:: byte numbers (single byte)
Readers interested in more Python related content can check out this site's topic: theSummary of Python string manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Python list (list) manipulation techniques summarized》、《Summary of Python coding manipulation techniques》、《Summary of Python function usage tipsand thePython introductory and advanced classic tutorials》
I hope that what I have said in this article will help you in Python programming.