5.格式化字符串

2020-05-18  本文已影响0人  only_Fisher

============================================================================
我们现在要键入更多的变量并且把它们打印出来。这次我们将使用一个叫“格式化字符串(format string)”的东西. 每一次你使用 " 把一些文本引用起来,你就建立了一个字符串。
使用专门的格式和语法把变量的内容放到字符串里,相当于来告诉 python :“嘿,这是一个格式化字符串,把这些变量放到那几个位置。”
============================================================================
意思就是先建立字符串,然后 使用专门的格式和语法把变量的内容放到字符串里
1.建立字符串?
我='fisher' 这就是一个
代号='asdfsd' 这也是一个
a='hello world' 任然是
2.专门的格式和语法
print ("%s")%我
这我感觉是没什么问题但是出错了

我='fisher'
print ('%s')%我
Traceback (most recent call last):
  File "E:/pythonFirstpro/venv/Include/5.py", line 2, in <module>
    print ('%s')%我
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
%s
Process finished with exit code 1

意思是代码的第二行有错,错误原因是不支持的操作类型

后来我搜了很多资料也没解决,最后去群里问了大佬,原来只要把 %我 放在括号里就行了。

我='fisher'
代号='asdfsd'
a='hello world'
print ("我是%s"%我)
print ("代号是%s"%代号)
print ("我说%s"%a)
我是fisher
代号是asdfsd
我说hello world

我是一个学python的小白,如果有错误可以评论告诉我,不胜感激。

上一篇下一篇

猜你喜欢

热点阅读