current position:Home>Python review (format string)

Python review (format string)

2022-02-01 10:36:18 Lemon tea has no lemon

Use print() Functions can easily output various objects , but print() Function cannot output a complex format .Python Provides a string formatting method to deal with complex format strings .

% Symbols format strings

  • String formatting involves two concepts : Format and formatting , Where the format is in % start , The formatting operator uses % Represents replacing the format in the format string with an object , The resulting 1 A string .
  • The general form of string formatting is shown in the figure :

image.png

Common format characters % The meaning of

image.png

IN

name='Lily'
age=18
print(' My name is %s, This year, %d year '%(name,age))
# Convert multiple objects at once , These objects are represented as a tuple , The position corresponds to the formatted character one by one 
 Copy code 

OUT

 My name is Lily, This year, 18 year 
 Copy code 

format() Method format string

format() The way is through {} and : To replace the tradition % The way . The general form is shown in the figure :

image.png

format Method formatting , You can use positional parameters , Pass parameters according to location ; You can also reference location parameters through index values , as long as format Method can only have parameter values at the corresponding positions , Parameter index from 0 Start ;

IN

print(' My name is {}, This year, {} year '.format(' Zhang San ',18))
print(' My name is {0}, This year, {1} year '.format(' Zhang San ',18))
print(' My name is {1}, This year, {0} year '.format(18,' Zhang San '))
 Copy code 

OUT

 My name is zhang SAN , This year, 18 year 
 My name is zhang SAN , This year, 18 year 
 My name is zhang SAN , This year, 18 year 
 Copy code 

f-string Format character

f-string Format the string to f start , Followed by a string , The expression in the string is in curly braces {} wrap up , It will replace the calculated value of the variable or expression , Examples are as follows :

IN

name=' Zhang San '
age=18
print(f' My name is {name}, This year, {age} year ')
 Copy code 

OUT

 My name is zhang SAN , This year, 18 year 
 Copy code 

String interception

  • The interception of string is to take out the substring in the string . There are two ways to intercept : One is index str[index] Take out a single character ; The other is slicing str[[start]:[end]:[step]] Take out a piece of character . The slicing method is the same as that described in the list section .

  • The index of characters in a string is the same as that of a list , Two way indexing is possible . As shown in the figure :

image.png

IN

s='student'
print(s[0])
print(s[-1])
print(s[1:3])# Take out position 1 To the position 2 The characters of , Not including location 3 The characters of 
print(s[:3])# Take it out from head to head 2 The characters of 
print(s[-2:])# Take it out from the penultimate 2 All characters starting at a position 
print(s[:])# Take out all characters 
print(s[::2])# In steps of 2
 Copy code 

OUT

s
t
tu
stu
nt
student
suet
 Copy code 

string manipulation :

IN

str1 = 'hello, world!'
#  With built-in functions len Calculate the length of the string 
print(len(str1)) # 13
#  Get a copy of the initial of the string in uppercase 
print(str1.capitalize()) # Hello, world!
#  Get a capital copy of each word in the string 
print(str1.title()) # Hello, World!
#  Get a copy of the capitalized string 
print(str1.upper()) # HELLO, WORLD!
#  Find the location of the substring from the string 
print(str1.find('or')) # 8
print(str1.find('shit')) # -1
#  And find Similar but no substring found will throw an exception 
# print(str1.index('or'))
# print(str1.index('shit'))

#  Check if the string starts with the specified string 
print(str1.startswith('He')) # False
print(str1.startswith('hel')) # True
#  Check if the string ends with the specified string 
print(str1.endswith('!')) # True
#  Centers the string with the specified width and fills both sides with the specified characters 
print(str1.center(50, '*'))
#  Places the string to the right with the specified width and fills the left with the specified characters 
print(str1.rjust(50, ' '))
str2 = 'abc123456'
#  Check if the string consists of numbers 
print(str2.isdigit())  # False
#  Check if the string is made up of letters 
print(str2.isalpha())  # False
#  Check that the string is made up of numbers and letters 
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 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 code 

copyright notice
author[Lemon tea has no lemon],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011036155882.html

Random recommended