SoFunction
Updated on 2024-11-16

A short summary of common examples of Python string manipulation

This article example describes the common operations of Python strings. Shared for your reference, as follows:

If we want to see the following features:help(mystr .find)

Example:

mystr="hello world itcast"
print(("world"))

prove

6

find brackets to fill in the contents of the search, if you can not find the return -1, to find the return from left to right to find the first location of the

The function is the same as find, except that it returns an error if it can't be found.

The first position you find from right to left.

Look from right to left.

Counts the number of occurrences in a string, returns 0 if no occurrence occurs.

Example:

("itcast")

prove

1

Replacement, parameter 1: source parameter 2: target But the original is not changed, only the result of the change is displayed once, because this is an immutable type, unless it is re-received with a variable

Example:

("world","WORLD")

Replace lowercase values with uppercase ones

cuts

Example:(" ") Cut out anything with spaces, cut by spaces, cut by what is there, what will be there is not there, save the format as a list

Make the first letter capitalized

mystr ='Hello world itcast'
print(())

Results for:

Hello world itcast

Capitalize every first letter of the string

mystr ='Hello World Itcast'
print(())

Results for:

Hello World Itcast

Checks if the string starts with a certain string, and if it does, returnstrueReturnsfalse  (obj)

Checks if a string ends with a certain string

Converts all uppercase characters in mystr to lowercase

Convert all lowercase characters in mystr to uppercase.

  rjust

Returns a new string that is left (right) aligned to the original string and padded to lengthwidth using spaces.

(10)Fill in the blanks if they are not long enough

Returns a new string with the original string centered and the length width padded with spaces.

rstrip strip

Remove whitespace from the front of the mystr string

Remove whitespace at the end of the mystr string

Remove whitespace characters from both ends of the mystr string

If you want to delete whitespace characters before and after several different strings and if there is a \t occurrence()Just don't add anything.

Split mystr into three parts by str, before str, after str and after str.

mystr='hello world itcast and it'
print(("itcast"))

Results for:

('hello world','itcast','and it')

lpartition

From the right and from the left

Returns a row-separated list containing each row as an element, cut by newlines.

mystr="hello\nworld"
print(())

Results for:

['hello','world']

Returns true if all characters of mystr are letters

()

Determine if a string is equal to a pure number

Is it a combination of letters and numbers in a string

Determine if it is a pure space

Concatenate strings together

Example:

names=["aaa","bb","cc"]
a="_"
(names)

Results for:

aaa_bb_cc

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.