Home >Backend Development >Python Tutorial >How to use str in Python with zero basic knowledge
Tools/Materials
python3.1.6
pycharm
Methods/Steps
# For strings: Insert a certain string into the middle of all the characters in the following string and concatenate it into a new string. s = '**'.join('good123')print(s)
print('****************** Slice**********************')s = 'good_make_dog_love_pig'c = s[-1] # Query the corresponding character according to the subscript, if you count from the left, start from 0 start. If counting from the right, start from -1. print(c)
# s[x:y:z] Starting from the x-th character, query to the y-th character (excluding y), z is the step long, the default step size is 1. s = 'good_make_dog_love_pig'res = s[0:10:2]print(res, type(res))
s = 'good_make_dog_love_pig'# s[x:y :z] Starting from the x-th character, query to the y-th character (excluding y), z is the step size, and the default step size is 1. res = s[0:10:2]print(res, type(res))
res = s[-1::-1] # The step size is a negative number , which means querying from right to left print(res, type(res))
print('************Encoding**** *********')a = ord('a') # Query the ASCII code of a certain character pair print(a, type(a))
c = chr(97) # Query the corresponding character (ASCII) according to the encoding print(c)c = 'h'print(chr(ord(c) 1))print('Given 2 lowercase letters, a
END
Notes
The seventh step is to introduce the random module, otherwise an error will be reported.
Related tutorial recommendations: Python video tutorial
The above is the detailed content of How to use str in Python with zero basic knowledge. For more information, please follow other related articles on the PHP Chinese website!