# 示例代码:使用 % 进行字符串格式化
name = "Alice"
age = 30
# 使用 %s 表示字符串,%d 表示整数
formatted_string = "My name is %s and I am %d years old." % (name, age)
print(formatted_string)
# 输出结果:
# My name is Alice and I am 30 years old.
# 示例代码:使用 % 进行取模运算
a = 10
b = 3
remainder = a % b
print("The remainder of %d divided by %d is %d" % (a, b, remainder))
# 输出结果:
# The remainder of 10 divided by 3 is 1
在 Python 中,% 有两种主要用途:
% 用于将指定的值插入到字符串中。例如,%s 用于字符串,%d 用于整数。% 用于计算两个数相除后的余数。上一篇:python print用法
下一篇:python中+=的用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站