String Formatting with %
来源:互联网 发布:2016cba选秀体测数据 编辑:程序博客网 时间:2024/05/16 08:34
Awesome work so far. This is the last new thing to cover before we review!
We saw earlier that you can access individual characters in a string by offset, or, if you want to think about it this way, ID number. (Remember, "PYTHON"[1]
is"Y"
, not "P"
!)
Unfortunately,strings in Python are immutable—you can't change them once they're created.
However, there is a way you can work flexibility into your strings, and that's with string formatting. It uses the %
symbol (don't confuse this with modulo!), and you can sort of think of it as a variable for your string.
Did you see that? The %
string formatter replaced the %s
(the "s" is for "string") in our string with the variables in parentheses. (We could have done that by just putting"Camelot"
and "place"
in parentheses after the string, but we wanted to show you how it works with variables.)
The syntax went like this:
print "%s" % (string_variable)
You can have as many variables (or strings!) separated by commas between your parentheses as you like:
print "The %s who %s %s!" % ("Knights", "say", "Ni")
prints "The Knights who say Ni!"
name = raw_input("What is your name?")quest = raw_input("What is your quest?")color = raw_input("What is your favorite color?")print "Ah, so your name is %s, your quest is %s, " \"and your favorite color is %s." %(name, quest, color)
- String Formatting with %
- String Formatting in C#
- String Formatting in C#
- Formatting String Objects
- String Formatting Operator
- 3.7.7 - String Formatting
- 【Python】Advanced String Formatting
- Formatting messages with variable content
- 字符串格式化(String Formatting)_C#
- Formatting
- not all arguments converted during string formatting
- not all arguments converted during string formatting
- SpringEmail with Velocity template date and number formatting
- Java for Complete Beginners StringBuilder and String Formatting
- not all arguments converted during string formatting 解决
- TypeError: not all arguments converted during string formatting
- TypeError: not all arguments converted during string formatting
- TypeError: not all arguments converted during string formatting问题解决
- 计算机视觉教材推荐
- hadoop作业调优参数整理及原理
- (英译中)Android Camera(二)
- Object Recognition and Scene Understanding(八)Matlab中Latent SVM model参数含义
- hdu 1712 ACboy needs your help一维+二维(最多只取一次的分组背包)
- String Formatting with %
- setsockopt ioctlsocket 设置非阻塞SOCKET函数
- jasperReports--IReport设计器编码
- Hibernate的Annotation中实体BLOB、CLOB类型的注解
- git 更改远程仓库
- 新游戏产业的5年之变
- mac下svn问题 —— “.a”(静态库)文件无法上传
- win32用GDI+加载png图片作为背景图
- Linux内核的三种调度策略