Slice Syntax#
sequence[start:stop]
The following t is equivalent to print
a ="12345"
print(a[0])#output the first one
print(a[-1])#output the last one
print(a[0:-1])#output from 0 to the last value (excluding the last value), if you change 0, -1 to 1, 5, it will output from 1 to the 6th
print(a[::-1])#reverse output
print(a[::2])#output every two, eg: 1 3 5
print(a[6:0:-1])#reverse output from the 7th to the 1st, if you change the parameters to 5, the output result is the same, because it outputs from the 6th to the 1st
String Syntax#
b=" 2, 3 ,a A "
print(b)
print(b.strip())#remove spaces on both sides
print(b.split(','))#split and separate characters with commas
print(b.upper())#convert all lowercase letters to uppercase letters
print(b.lower())#convert to lowercase letters
print(b.replace(",","!"))#replace commas with exclamation marks
Formatted Output#
a=input("Hello, please enter your name:")
b=int(input("Please enter your age:"))#need to convert the input to int type
print("Hello, my name is %s, and I am %d years old"%(a,b))
The input() function returns a string (str) type, and you used the %d format specifier to print the age, which is used for integers. If the input age is not a pure number, or if you want to ensure that the code is more robust, it is best to convert the input age string to an integer.
a=input("Hello, please enter your name:")
b=int(input("Please enter your age:"))
print("Hello, my name is {i_a}, and I am {i_b}".format(i_a=a,i_b=b))#pay attention to the curly braces, implemented through the .format() function