《Beginning Python From Novice to Professional》学习笔记一:String
来源:互联网 发布:剑雨江湖进阶最新数据 编辑:程序博客网 时间:2024/04/26 15:23
1.两个字符串放在一起会自动合并
"Let's say " '"Hello, world!"' --->'Let/'s say "Hello, world!"'
但是两个字符串变量放在一起中间要加“+”号才会合并
2.str与repr
str会将变量转化成某种用户能够理解的String方式,如
print str(10000L) --->10000
#str(object) -- Converts a value to a string
repr会将变量转化为Python的合法变量
print repr(10000L) --->10000L
另外repr(x)='x'
#repr(object) -- Returns a string representation of a value
3.input与raw_input
name = input("What is your name? ")回车继续
What is your name? Gumby
会出错,因为input会假设你输入的是有效的Python变量,而此处Gumby未定义。改为
What is your name? 'Gumby'则成功
#input(prompt) -- Gets input from the user
name = raw_input("What is your name? ")回车继续
What is your name? Gumby 则不会报错,因为raw_input会认为输入的均为String
#raw_input(prompt) -- Gets input from the user, as a string
4.'''与r与u
'''长字符串''' 中间可以分行,可以任意用'、"
r前缀为Raw String, 'Hello /nthyU!'会分行,而r'Hello /nthyU!'就忠实于WhatYouSeeIsWhatYouGet,
u前缀为Unicode String,每个字符用16Bit。
"Let's say " '"Hello, world!"' --->'Let/'s say "Hello, world!"'
但是两个字符串变量放在一起中间要加“+”号才会合并
2.str与repr
str会将变量转化成某种用户能够理解的String方式,如
print str(10000L) --->10000
#str(object) -- Converts a value to a string
repr会将变量转化为Python的合法变量
print repr(10000L) --->10000L
另外repr(x)='x'
#repr(object) -- Returns a string representation of a value
3.input与raw_input
name = input("What is your name? ")回车继续
What is your name? Gumby
会出错,因为input会假设你输入的是有效的Python变量,而此处Gumby未定义。改为
What is your name? 'Gumby'则成功
#input(prompt) -- Gets input from the user
name = raw_input("What is your name? ")回车继续
What is your name? Gumby 则不会报错,因为raw_input会认为输入的均为String
#raw_input(prompt) -- Gets input from the user, as a string
4.'''与r与u
'''长字符串''' 中间可以分行,可以任意用'、"
r前缀为Raw String, 'Hello /nthyU!'会分行,而r'Hello /nthyU!'就忠实于WhatYouSeeIsWhatYouGet,
u前缀为Unicode String,每个字符用16Bit。
- 《Beginning Python From Novice to Professional》学习笔记一:String
- 《Beginning Python From Novice to Professional》学习笔记二:Sequence
- 《Beginning Python From Novice to Professional》学习笔记三:List
- 《Beginning Python From Novice to Professional》学习笔记四:Tuple
- 《Beginning Python From Novice to Professional》学习笔记六:Dictionary
- 《Beginning Python From Novice to Professional》学习笔记七:Statement
- 《Beginning Python From Novice to Professional》学习笔记八:Abstraction
- 《Beginning Python From Novice to Professional》学习笔记十:Exception
- 《Beginning Python From Novice to Professional》学习笔记十一:__Magic__
- 《Beginning Python From Novice to Professional》学习笔记十二:__Magic__
- 《Beginning Python From Novice to Professional》学习笔记五:Advanced String
- Beginning Python: From Novice to Professional
- 《Beginning Python From Novice to Professional》学习笔记九:More Abstraction
- Beginning Python From Novice to Professional (3) - 列表操作
- Beginning Python From Novice to Professional (6) - 函数使用
- Beginning Python From Novice to Professional (7) - 类
- Beginning Python From Novice to Professional (8) - 文件方法
- Beginning Python From Novice to Professional (9) - Socket
- Web Services通常有两种实现形式
- 爱需要感觉、爱情需要时间
- C语言实现N皇后问题源代码
- JAX-WS 介绍 【转载】
- 如何让J2EE开发速度更快
- 《Beginning Python From Novice to Professional》学习笔记一:String
- informix 中创建DateTime类型字段
- ClickOnce的prerequisite组件安装报错
- 让LoadRunner走下神坛
- 回归RVDS
- 《Beginning Python From Novice to Professional》学习笔记二:Sequence
- 《Beginning Python From Novice to Professional》学习笔记三:List
- unbuntu倒计时
- 微软视频下载网站