SoFunction
Updated on 2024-11-10

Python String Common Functions Explained

String common functions:

declare a variable

str="Hello World"

find() detects whether a string is contained, returns the position of the string, and -1 if it is not contained.

("Hello") # Return value: 0
("W") # Return value: 6, Note that a space is also a character, and there is a space before W, so the W position is 6.
("R") # return value:-1,is not included in theHello Worldcenter,If it does not contain return-1

index() detects whether the string contains the specified character and returns the beginning of the index value; if it does not, an error is reported.

("Hello") # Return value: 0
("o") # Return value: 4
("W") # Return value: 6
("R") # return value:error message (computing) ,on account ofRIt's not included.。 So it's advisable to use it with caution,If the value doesn't exist, the program is screwed.。

len() Returns the length of the string, counting from 0.

len(str) #Return value: 10

count() collects the number of times the specified character appears in the string

("o") return value:2, ocharacters in theHello WorldThere are two。

# You can also specify that the count() function starts at a certain position. The syntax is: count(" ",start,end)
('o',5,10) return value:1, rationale:Specifying a location will result in a change from the index5Start searching,indexing by10close。 5until (a time)10There is only one'o'
('o',4,len(str)) return value: 2,Indexing from4commencement,until (a time)字符串close。len(str)String length

replace() replaces the string

('hello','HELLO')  # Replace lowercase hello with uppercase HELLO
('W','B')  # particle marking the following noun as a direct objectWReplace withB

split() String split

('o') # Returns ["hell", "w", "rld"] as a list, with the o's inside hello world cut off.

upper() converts all characters to upper case

() # Return value is HELLO WORLD

title() converts initials to uppercase

() #Return value: Hello World

center() returns a new string with the original string centered and padded with spaces to the width of the length.

(80) #Return value: ( Hello World ) whose string is padded at both ends with spaces

join() constructs a new string by inserting a specified character after the string.

_str="_" 
list=["I","Love","You"]
_str.join(list) # return value: I_Love_You An underscore is inserted after each list element.

isspace() detects whether the string contains only spaces, if so return Trun return False, in layman's terms is to determine the non-null verification

str=" "
strOne="Good morning!"
() # Return trun
 #come (or go) backfalse

isalnum() Detects if the password contains only numbers or letters. Usage: can be used to determine the password, in general the password can not enter Chinese characters or spaces.

strOne="a123"
strTwo="a 456"
() # Return trun
() # come (or go) backfalse ,Because it contains spaces

isdigit() detects if a character contains only digits, returning Trun and False.

str='123'
strone='a123'
() come (or go) backtrun 
() come (or go) backfalse

isalpha() detects if a string contains only letters.

str="abcd"
strone="123abacd"
() # Return trun
() # Returns false

summarize

The above is a small introduction to the python string of commonly used functions, I hope to help you, if you have any questions, please leave me a message, I will reply to you in a timely manner. Here also thank you very much for your support of my website! If you feel that this article is helpful to you, welcome to reproduce, bother to indicate the source, thank you!