Python数据类型:
int(整数):1 2 3 4 ......
float(浮点数):3.12 1.33 ......
string(字符串):'hello', 'book' ......
#如果字符串内部既包含'又包含"怎么办?
#可以用转义字符\来标识,比如:
'I\'m \"OK\"!'
# 字符串可以用 +操作符联接,也可以用* 操作符重复多次
>>> 3 * 'un' + 'ium'
'unununium'
变量赋值:
# 等号 (=) 用于给变量赋值。
# 如果变量没有“定义”(赋值),使用的时候将会报错
>>> width = 20
>>> height = 5*9
>>> width * height
900
# 如果你前面带有\的字符被当作特殊字符,你可以使用原始字符串,方法是在第一个引号前面加上一个r:
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
字符串可以跨多行。一种方法是使用三引号:”””…”””或者”’…”’。行尾换行符会被自动包含到字符串中,但是可以在行尾加上 \ (续行符)来避免这个行为。
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
字符串也支持索引和切片
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'