变量和简单数据类型
2.2 变量
Python是动态语言。
>>> a = 123
>>> print a
123
>>> a = 'ABC'
>>> print a
ABC2.2.1 变量的命名和使用
2.3 字符串
在Python中,用引号括起来的都是字符串,其中的引号可以是单引号,也可以是双引号。
2.3.1 大小写转换
>>> name = "ada lovelace"
>>> print(name.title())
Ada Lovelace
>>> print(name.upper())
ADA LOVELACE
>>> print(name.lower())
ada lovelace2.3.2 拼接字符串
2.3.3 使用制表符或换行符来添加空白
2.3.4 删除空白
2.3.5 使用字符串时避免语法错误
2.3.6 Python2中的print语句
在Python2中,无需将要打印的内容放在括号内。从技术上说,Python3中的print是一个函数,因此括号必不可少。有些Python2的print语句也可包含括号,但其行为与Python3中稍有不同。简单地说,在Python2代码中,有些print语句包含括号,有些不包含。
2.3.7 占位符
常见占位符
占位符
%d
整数
%f
浮点数
%s
字符串
%x
十六进制整数
2.3.8 其他方法
2.4 数字
2.4.1 整数
进制转换
2.4.2 浮点数
2.4.3 使用函数str()避免类型
调用函数str()将非字符串值表示为字符串:
参考
Last updated
Was this helpful?