在Python中使用 % 格式化字符串
在Python中,可以通过不同的方法来实现所需的格式化字符串。
他们之中有一些是 ;
1) 使用 %
2) 使用 {}
3) 使用模板字符串
本文讨论了使用 % 的格式化。
使用 % 的格式类似于 C 编程语言中的 'printf'。
%d – 整数
%f – 浮动
%s –字符串
%x – 十六进制
%o - 八进制
下面的示例描述了在Python中使用 % 进行格式化的用法
# Python program to demonstrate the use of formatting using %
# Initialize variable as a string
variable = '15'
string = "Variable as string = %s" %(variable)
print (string )
# Printing as raw data
# Thanks to Himanshu Pant for this
print ("Variable as raw data = %r" %(variable))
# Convert the variable to integer
# And perform check other formatting options
variable = int(variable) # Without this the below statement
# will give error.
string = "Variable as integer = %d" %(variable)
print (string)
print ("Variable as float = %f" %(variable))
# printing as any string or char after a mark
# here i use mayank as a string
print ("Variable as printing with special char = %cmayank" %(variable))
print ("Variable as hexadecimal = %x" %(variable))
print ("Variable as octal = %o" %(variable))
输出 :
Variable as string = 15
Variable as raw data = '15'
Variable as integer = 15
Variable as float = 15.000000
Variable as printing with special char = mayank
Variable as hexadecimal = f
Variable as octal = 17