python赋值种类_Python的数据类型及变量赋值

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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值