Basic operations for python strings

Keywords: Python less

Learning documentation included with python: enter the command pyhton3-m pydoc-p 8888 (port number, self-defined), and then enter localhost:8888 where the web address is entered in the browser
Member operations on strings

#Member operations on strings
s1 = "Hello String"
s2 = "H"

#Inclusion operation in
print(s2 in s1)

#Include operation not in 
print(s2 not in s1)

True
False

Escape Character
What if the string contains'or'inside?We can use \ for escaping, such as print('I'm'OK'), and \ for many characters, such as \n for line breaks, \t for tabs, and so on.

#Escape Character
print("\'")
print("\"")

#Line Break\n
print("Hello\nPython")

#Tab tab \t
print("Hello\tPython")  #Four spaces added

#Enter\r
print("Hello\rPython")  #Move the cursor to the beginning of the line, after printing\r

#Output a string: Hello\nPython
#Original string
print(r"Hello\nPython")
print(R"Hello\nPython")  #Prepending a string with R or R outputs the original string

'
"
Hello
Python
Hello Python
Python
Hello\nPython
Hello\nPython

Formatted output of strings

#Formatted output of strings
print("My name is%s, Today is my first day%d Day study python!" % ("Xiao Ming", 10))
print("My name is{}, Today is my first day{}Day study python!".format("Xiao Ming", 10))

My name is Xiao Ming. Today is my 10th day to learn python!
My name is Xiao Ming. Today is my 10th day to learn python!

Built-in functions for Strings

#Built-in functions for Strings

#Find string str.find()
s = "Hello Python".find("o") # Return to the location of the first'o'found
print(s)

#Convert to lowercase str.lower()
print("Hello Python".lower())

#Convert to uppercase str.upper()
print("Hello Python".upper())

#Returns the string length str. u len_() This function returns the natural length of the string, calculated from the beginning 
print("Hello Python".__len__())

#Determines whether a string contains only spaces str.isspace()
print(" ".isspace())

#String substitution str.replace()
print("Hello Python".replace("o", "ee")) #Replace o with ee

4
hello python
HELLO PYTHON
12
True
Hellee Pytheen

str = "helloWorld"  #Strings can be assigned directly
x = len(str)  #Calculate String Length
y = min(str)  #Minimum String
z = max(str)  #Maximum String
print("String length is{},Minimum string size is{},Maximum string size is{}".format(x,y,z))
a = "hello, "
b = "World"
print("a + b = {}".format(a + b)) #addition
print("3 * a = {}".format(3 * a)) 

s = 'hello'
print(s.capitalize()) #Returns a string in upper case
print(s.upper()) #Converts lowercase letters in a string to uppercase
print(s.rjust(7)) #str.rjust(width[, fillchar]),width -- Specifies the total length of the string after the specified character is filled, fillchar -- The filled character, defaults to space
#Returns a new string with the original string right-aligned and spaces filled to the length of the width.Returns the original string if the specified length is less than the length of the string.
print(s.center(7)) #str.center(width[, fillchar]),width -- the total width of the string, fillchar -- the fill character
#Returns a new string with the original string centered and spaces filled to the length of the width.Default fill character is space
print(s.replace('e', 'a')) #Replace old (old string) with new (new string) in the string
print('world'.strip()) #Used to remove characters specified at the beginning and end of a string (default is space or line break) or character sequences

Some of the built-in methods for string are defined in Python to determine the components of a string.
string.isalnum() returns True if a string has at least one character and all characters are letters or numbers, or False if they are not.

string.isalpha() returns True if a string has at least one character and all characters are letters, or False if not.

string.isdecimal() Returns True if string contains only decimal digits or False if string does not.

string.isdigit() Returns True if string contains only numbers or False if string does not.

string.islower() Returns True if a string contains at least one case-sensitive character and all of these (case-sensitive) characters are lowercase, or False if they are not.

string.isnumeric() Returns True if a string contains only numeric characters, or False if it does not.

string.isspace() returns True if the string contains only spaces, or False if it does not.

string.istitle() returns True if the string is titled (see title()) or False if it is not.

string.isupper() Returns True if a string contains at least one case-sensitive character and all of these (case-sensitive) characters are uppercase, or False if they are not.

You can be here File More string methods found in

Posted by magi on Thu, 25 Jul 2019 20:16:41 -0700