String formatting in Python: How to use the format() function

王林
Release: 2023-04-22 19:01:05
forward
1566 people have browsed it

变量插入字符串的方法

Python中的format()函数是一种将变量插入字符串的方法,能够使字符串更易于阅读和理解。它支持许多不同的用法,以下是具体的用法和说明:

  • 使用位置参数传递变量

name = 'John'
age = 25
print('My name is {}, and I am {} years old.'.format(name, age))
# 输出:My name is John, and I am 25 years old.
Copy after login
  • 使用索引传递变量

name = 'John'
age = 25
print('My name is {0}, and I am {1} years old.'.format(name, age))
# 输出:My name is John, and I am 25 years old.
Copy after login
  • 使用关键字参数传递变量

name = 'John'
age = 25
print('My name is {n}, and I am {a} years old.'.format(n=name, a=age))
# 输出:My name is John, and I am 25 years old.
Copy after login
  • 格式化数字

price = 19.99
print('The price is ${:.2f}'.format(price))
# 输出:The price is $19.99
Copy after login
  • 对齐文本

text = 'Hello'
print('{:>10}'.format(text))  # 右对齐输出,总宽度为10
# 输出:     Hello
print('{:^10}'.format(text))  # 居中输出,总宽度为10
# 输出:  Hello   
print(&#39;{:<10}&#39;.format(text))  # 左对齐输出,总宽度为10
# 输出:Hello
Copy after login
  • 使用格式化字符串(Python 3.6及以上版本)

name = &#39;John&#39;
age = 25
print(f&#39;My name is {name}, and I am {age} years old.&#39;)
# 输出:My name is John, and I am 25 years old.
Copy after login
  • 使用字典传递变量

person = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}
print(&#39;My name is {name}, and I am {age} years old.&#39;.format(**person))
# 输出:My name is John, and I am 25 years old.
Copy after login
  • 使用下标操作符获取列表中的元素

fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]
print(&#39;My favorite fruit is {0[1]}&#39;.format(fruits))
# 输出:My favorite fruit is banana
Copy after login
  • 使用花括号转义

print(&#39;{{Hello}}&#39;.format())  # 输出:{Hello}
Copy after login
  • 使用冒号分隔格式字符串和变量名称,对变量进行进一步格式化

name = &#39;John&#39;
score = 95
print(&#39;Student: {0:&lt;10} Score: {1:.2f}&#39;.format(name, score))
# 输出:Student: John       Score: 95.00
Copy after login
  • 根据变量类型自动选择格式

x = 42
y = 3.14
print(&#39;x is {!r}, y is {!s}&#39;.format(x, y))
# 输出:x is 42, y is 3.14
Copy after login
  • 使用填充字符

x = 42
print(&#39;{:0&gt;5}&#39;.format(x))  # 右对齐,用 0 填充,总宽度为 5
# 输出:00042
Copy after login
  • 根据变量类型选择不同的进制输出

x = 42
print(&#39;bin: {0:b}, oct: {0:o}, hex: {0:x}&#39;.format(x))
# 输出:bin: 101010, oct: 52, hex: 2a
Copy after login
  • 自定义格式化函数

def format_salary(salary):
    if salary > 10000:
        return &#39;{:.1f}K&#39;.format(salary / 1000)
    else:
        return &#39;${:,.2f}&#39;.format(salary)
print(format_salary(5000))   # $5,000.00
print(format_salary(15000))  # 15.0K
Copy after login
  • 使用 ** 和 * 进行动态参数传递

data = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25}
print(&#39;{name} is {age} years old.&#39;.format(**data))  # John is 25 years old.
fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]
print(&#39;My favorite fruits are {}, {} and {}.&#39;.format(*fruits))  # My favorite fruits are apple, banana and cherry.
Copy after login

The above is the detailed content of String formatting in Python: How to use the format() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template