如何用%在Python里替换字符串的部分内容

2025-11-04 00:53:28

1、新建一个空白的PYTHON文件。

如何用%在Python里替换字符串的部分内容

2、先定义一个变量,然后把字符串内容改为另一个字符串

x = "Ben"

y = "My name is %s" %x

print(y)

如何用%在Python里替换字符串的部分内容

3、可以把字符串里的改为整型数字。

a = 3

b = "The first number is %d" %a

print(b)

如何用%在Python里替换字符串的部分内容

4、%f把整数改为有小数点的数值

c = 9

d = "You are the number %f guy" %c

print(d)

如何用%在Python里替换字符串的部分内容

5、当然可以把有小数点的值改一下即可。

e = 8.987987234

f = "The answer is %f" %e

print(e)

如何用%在Python里替换字符串的部分内容

6、如果不定义变量,其实也可以修改里面的内容的。

g = "The winner is %s" %"Alice"

print(g)

如何用%在Python里替换字符串的部分内容

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:85
  • 阅读量:159
  • 阅读量:147
  • 阅读量:46
  • 阅读量:167
  • 猜你喜欢