String inPythonIs a series of characters enclosed in quotation marks or double quotation marks:
You can assign string values to variables:
name = "Roger"
You can use to concatenate two strings
phrase = "Roger" + " is a good dog"
You can use the following to append to the string
name = "Roger" name += " is a good dog"
print(name) #Roger is a good dog
you can use it
strThe constructor of the class:
This is essential for concatenating numbers to strings:
print("Roger is " + str(8) + " years old") #Roger is 8 years old
When using a special syntax definition, the string can be multi-line and enclosed in 3 quotation marks:
print("""Roger is 8 years old """)
#double quotes, or single quotes
print(’’’ Roger is 8 years old ‘’’)
Strings have a set of built-in methods, for example:
isalpha()Check if the string contains only characters and is not empty
isalnum()Check whether the string contains characters or numbers, and is not empty
isdecimal()Check if the string contains numbers and is not empty
lower()Get the lowercase version of the string
islower()Check if the string is lowercase
upper()Get the uppercase version of a string
isupper()Check if the string is uppercase
title()Get the uppercase version of the string
startsswith()Check if the string starts with a specific substring
endswith()Check if the string ends with a specific substring
replace()Replace part of the string
split()Split a string on a specific character delimiter
strip()Trim white spaces in strings
join()Append new letter to string
find()Find the position of the substring
There are a lot more.
None of these methods change the original string. They return a new, modified string. E.g:
name = "Roger" print(name.lower()) #"roger" print(name) #"Roger"
You can also use some global functions to process strings.
I especially thought of
len(), It gives you the length of the string:
name = "Roger" print(len(name)) #5
inOperators allow you to check whether a string contains substrings:
name = "Roger" print("ger" in name) #True
Escaping is a method of adding special characters to a string.
For example, how to add double quotes to a string wrapped in double quotes?
name = "Roger"
"Ro"Ger"Won't work, because Python will think that the string starts with
The feasible method is to use the string to escape the double quotes in the string.
name = "Ro\"ger"
This also applies to single quotes
\', And characters in special formats, such as
\nFor new line and
Given a string, you can use square brackets to get its characters to give a specific item of its index starting from 0:
name = "Roger" name #'R' name #'o' name #'g'
Use negative numbers to count from the beginning:
name = "Roger" name[-1] #"r"
You can also use ranges, the so-calledslice:
name = "Roger" name[0:2] #"Ro" name[:2] #"Ro" name[2:] #"ger"
More python tutorials:
- Introduction to Python
- 在macOS上安装Python 3
- Run Python program
- Python 2 and Python 3
- Basics of using Python
- Python data types
- Python operators
- Python string
- Python boolean
- Python numbers
- Python, accepts input
- Python control statements
- Python list
- Python tuple
- Python set
- Python dictionary
- Python functions
- Python objects
- Python loop
- Python module
- Python class
- Python standard library
- Debug Python
- Python variable scope
- Python, accept parameters from the command line
- Python recursion
- Python nested functions
- Python Lambda function
- Python closure
- Python virtual environment
- Use Python to use GoPro as a remote webcam
- Python, how to create a list from a string
- Python decorator
- Python Docstrings
- Python introspection
- Python notes
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get detailed information of a file
- Python, how to check if a file or directory exists
- Python exception
- Python, how to create a directory
- Python, how to create an empty file
- Python, `with` statement
- Python, create a network request
- Python, use `pip` to install third-party software packages
- Python, read file content