SoFunction
Updated on 2025-03-02

Complete collection of methods and operations of python strings

1: Methods and operations of strings

*Note: The first letter l is operated from the left, and the method of r is operated from the right

1.__contains__() determines whether it contains

Determine whether the specified character or string is contained in a string, and the return value is true or false

str1="asdfgh"
print(str1.__contains__('a'))
print(str1.__contains__("df"))
print(str1.__contains__('r'))

Running results:

True

True

False

The function is similar to that of in

str1="asdf"
print('s' in str1)

Running results:

True

2.__eq__() equal

Determine whether the two strings are equal, and the return value is True or False

str1="asdf"
print(str1.__eq__("asdf"))
print(str1.__eq__("addfd"))

Running results:

True

False

3. You can use %s+ to add strings

str1="asd"
str2="fgh"
str3=str1+str2
str4="%s%s"%(str1,str2)
print(str3)
print(str4)

Running results:

"asdfgh"

"asdfgh"

String stitching

str1="as{0}dsz{1}"
result=("hu","ui")
print(result)
str2="as{id}dsfdfz{name}"
result=(,name="ui")
#The variables in format cannot be used outsideprint(result)

Running results:

"ashudszui"

"ashudsfdfzui"

() string initial letter capitalization

str1="asdfg"
print(())

Running results:

"Asdfg"

()Lowercase of the first letter

str1="ASDFG"
print(())

Running results:

"aSDFG"

() Center the content two parameters

#You can have one parameter or two parameters. The following parameter is a filler character, and the default is a space

str1="sdfg"
print((20))
print(str1.center30,'*'))

Running results

sdfg       
*************sdfg*************

()coding

Change string encoding

str1="Lan Yanru"
print(("gbk"))

Running results:

b'\xc0\xbc\xd1\xde\xc8\xe3'

() Determine whether a string ends with a certain character

str1="asdfdgdghfh"
print(('h'))
print(('e'))

Running results:

True
False

() Convert tab to space\t

*I don't think I'm useful

str1="sdfdf\t1ws"
print(str1)
print(())

Running results:

sdfdf 1ws
sdfdf   1ws

Find the position of a character in the string. If not, display -1, and you can add the starting position and end position.

str1="sdgfhfh"
print(('h'))
print(('a'))

Running results:

4
-1

Return to location

Return the position of the character in the string. If it is not found, an error will be reported.

str1="sdgfhfh"
print(('h'))
print(('a'))

Running results:

4
Traceback (most recent call last):
 File "/usercode/", line 8, in 
  print(('a'))
ValueError: substring not found

() is used to splice, "" represents the separator, which can be defined

str1=['s','o','n','g',]
print("".join(str1))
print(str1)
print("-".join(str1))

Running results:

song
['s', 'o', 'n', 'g']
s-o-n-g

() Put it on the left, same as the center

Like a center, position the string in a line. ljust starts from the left and the parameter is the length from the left.

str1="qeretry"
print((10,'+''))
print((20,'-'))
print((30))
print((30,'*'))

Running results:

qeretry+++
qeretry-------------
qeretry            
qeretry***********************

()lower case

All lowercase

str1="AsdFGd"
print(())
print(str1

Running results:

asdfgd
AsdFGd

() Remove the space on the left

str1=" ddfd "
print(())

Running results:

ddfd

() and translate() methods

These two methods need to be compared and combined

str1="12345"
str2="asdfg"
aa="afgjdfhd"
makes=(str2,str1)
print((makes))

Running results:

145j34h3

("Segmented Characters")

str1="woaipython"
print(("ai"))

Running results:

('wo', 'ai', 'python')

()replace

('Old Characters','New Character')
('Old Characters','New Character',‘How many to convert')
str1="asdfghjkladadafgasag"
print(('a','p'))
print(('a','q',3))

Running results:

psdfghjklpdpdpfgpspg
qsdfghjklqdqdafgasag

()

The method of application is the same as find, the difference is to search from right to left

()

The method of application is the same as ljust above, the difference is to search from right to left

() Specify characters and split strings

The specified characters will be deleted

str1="qwetatrassongsdchengxcxu"
print(('s'))

Running results:

['qwetatra', '', 'ong', 'dchengxcxu']

() is split according to newline characters, which is equivalent to split('\n')

str1='''"aa""bb""cc"
'''
print(())
str1='''"aa"
"bb"
"cc"
'''
print(())

Running results:

['"aa""bb""cc"']
['"aa"', '"bb"', '"cc"']

What does () start with

Determines whether a string starts with a character or string

str1="adgdfgsdf"
print(('a'))
print(("ad"))
print(("ddd"))

Running results:

True
True
False

()Case conversion, large becomes smaller, small becomes larger

str1="dsDDfFDSSSSSFFqqq"
print(())

Running results:

DSddFfdsssssffQQQ

() Convert the string to the title, that is, the first letter is capitalized

str1="dkjgdkgj"
print(())

Running results:

Dkjgdkgj

Two: Summary

1. Common methods

center(),startswith(),ljust(),rjust(),__eq__(),partition(),replace(),rsplit(),splitlines(),lstrip(),rstrip(),strip(),join(),index(),format()

2. Pay attention to developing habits: whether it is a tuple, list, or dictionary, add a comma eg after the element:str=['1','a',]

Summarize

The above is a complete collection of python string methods and operations introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!