Python 学习笔记(七)Python字符串(四)

输入输出

输入函数 raw_input (Python3:input)

 >>> raw_input("请输入一个字母") #获取输入内容的一个函数
请输入一个字母A
'A'
>>>
>>> name = raw_input("Please input your name:") #输入得内容赋值给一个变量
Please input your name:tom
>>> name
'tom'
>>> x =raw_input("how old are you?")
how old are you?x =raw_input("how old are you?")
>>> x =raw_input("how old are you?")
how old are you?22
>>> x
''
>>> type(x) #通过raw_input 得到的结果是一个字符串类型
<type 'str'>
>>> help(raw_input) #查看raw_input详细内容
Help on built-in function raw_input in module __builtin__: raw_input(...)
raw_input([prompt]) -> string 返回值字符串 Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.

print()格式化

print 在python2中是一个语句 ,在python3中print() 是一个函数

格式化   指定一种格式,按照某种格式打印出来,

    制定一种模板,在模板中某个地方留出一些空位,在空位上填上某些内容,那么这些空位就需要一些符号来表示,通常称之为占位符

%s  字符串占位符

%d  整数占位符

%f  浮点数占位符

 >>> "I LIKE %s"
'I LIKE %s'
>>> "I Like %s"%"Python" #用%之后的Python 来填充%s这个占位符
'I Like Python'
>>> a = "I Like %s" >>> a %"java"
'I Like java'
>>> print "%s was born in %d "%("tom",2018) #括号中为字符串和数字
tom was born in 2018
>>>

用花括号{}索引值 占位符 Python 推荐使用  {}

   str.format() 格式化

 >>> s = "I like {0}"
>>> s.format("Python")
'I like Python'
>>> s2 ="%s was born in %d "%("tom",2018)
>>> s2.format("tom",2018)
'tom was born in 2018 '
上一篇:out ref区别


下一篇:win7下给右键菜单添加启动cmd命令