It’s easy to print various objects using the print() function, but print() can’t print a complex format. Python provides string formatting to handle complex strings.

The % symbol formatting string

  • String formatting involves two concepts: format and formatting, where format begins with % and the formatting operator uses % to represent replacing the format in the format string with an object, resulting in a string.
  • The general form of string formatting looks like this:

Meaning of the common format character %

IN

Name ='Lily' age=18 print(' I am %s, this year %d '%(name,age)) # print(' I am %s, this year %d '%(name,age)Copy the code

OUT

My name is Lily and I am 18 years oldCopy the code

The format() method formats a string

The format() method uses {} and: instead of the traditional % method. The general form is shown in the figure:

When formatting, the format method uses positional arguments that are passed by position. Positional parameters can also be referenced by index values, as long as the format method has a value at the corresponding position. The parameter index starts at 0.

IN

Print (' my name is {}, {} '. The format (' zhang SAN, 18)) print (' this year, my name is {0} {1} 'age. The format (' zhang SAN, 18)) print (' this year, my name is {1}, {0}' age. The format (18, 'zhang'))Copy the code

OUT

My name is Zhang SAN. I am 18 years old. My name is Zhang SANCopy the code

F-string Format character

An f-string format string begins with f followed by a string. The expression in the string is enclosed in curly braces {}, which replaces the evaluated value of a variable or expression, as shown in the following example:

IN

Print (name=' id ', age=18 print(name=' id ', age=18)Copy the code

OUT

My name is Zhang SAN and I am 18 years oldCopy the code

String interception

  • String interception is the extraction of a string of substrings. There are two methods of interception: one is the index STR [index] to extract a single character; Alternatively, slice STR [[start]:[end]:[step]] to extract a single character. Slicing is the same as described in the list section.

  • The index of a character in a string, like a list, can be indexed bidirectionally. As shown in the figure:

IN

S ='student' print(s[0]) print(s[-1]) print(s[1:3]) Does not include the character position 3 print (s) [3] : 2 # remove from the beginning to the position of print characters (s / - 2:) # remove from the bottom second position of all characters start print (s) [:] # remove all print the characters (s) [2] : : # step length is 2Copy the code

OUT

s
t
tu
stu
nt
student
suet
Copy the code

String handling:

IN

str1 = 'hello, world! Print (str1.capitalize()) print(str1.capitalize()) # Hello, world! Print (str1.title()) # Hello, World! Print (str1.upper()) # HELLO, WORLD! Print (str1.find('shit')) # 8 print(str1.find('shit')) # -1 # print(str1.index('or')) # -1 Print (str1.startswith('He')) # False print(str1.startswith('hel')) # Print (str1.endswith('! Print (str1.center(50, '*')) # Print (str1.rjust(50, '*') # print(str1.rjust(50, Print (str2.isdigit()) # False # Print (str2.isalpha()) # False # Print (str2.isalnum()) # True str3 = '[email protected]' print(str3) # get a copy of the string after trimming the left and right Spaces print(str3.strip())Copy the code

OUT

13 Hello, world! Hello, World! HELLO, WORLD! 8 -1 False True True ******************hello, world! ******************* hello, world! False False True [email protected] [email protected]Copy the code