不同类型的数据不能拼接在一起。一句话:圈子不同不相融。然而又解决办法:
负责转换数据类型的函数一共有3种:str()、 int()和float()。
能帮我们解决“不同类型的数据不能拼接在一起”的代码Bug,就是str()函数,它拥有镜像世界的『影分身术』,可让其他类型的数据分身成str类型。
str()函数

str()函数能将数据转换成其字符串类型,不管这个数据是int类型还是float类型,只要放到括号里。这个数据就能摇身一变,成为字符串类型。
1 2 3 4 5 6 7 |
who = '我的' action = '是' destination = '镜像世界' number = 153 code = '通行密码' print(who+destination+code+action+str(number)) |
我们只需通过str(number)一个步骤,便可以将整数类型的【153】转化为字符串类型的【153】,成功完成数据拼接。
1 2 3 4 5 6 7 |
magic = '水仙花数' action = '是' unit = '个' expression = '=1x1x1+5x5x5+3x3x3' number = 153 print(str(number)+expression+action+unit+magic) |
成功打印出结果153=1x1x1+5x5x5+3x3x3是个水仙花数。
另一种方法:
1 2 3 4 5 6 7 8 |
magic = '水仙花数' action = '是' unit = '个' expression = '=1x1x1+5x5x5+3x3x3' number = 153 print('153'+expression+action+unit+magic) |
两种不同的写法:str()函数和引号,输出同一种结果
因为,当我们使用引号时,引号里的东西,都会被强制转换为字符串格式。
所以,如果我们把变量名number放进引号里后,被强制转换为字符串的,将是变量名number,而不是变量名代表的数字153。
编程思维——条条大路通罗马。
代码的世界千变万化,同一个结果可能有千百万种答案,如果我们只心安理得地,局限在一种思维模式下。那这个思维定势一定会阻挡你的进步,阻止你成为真·大佬。
所以,尽情创新,尝试不同的选择,就像老祖宗说的:胆大心细,绝对没错。
1 2 3 4 5 6 7 8 9 |
place1 = '镜像世界的' unit = '天' place2 = '现实世界的' action = '等于' number1 = 1 number2 = 666 print(place1+str(number1)+unit+action+place2+str(number2)+unit) print(place1+'1'+unit+action+place2+'666'+unit) |
1 2 3 |
#输出结果: 镜像世界的1天等于现实世界的666天 镜像世界的1天等于现实世界的666天 |
int()函数

将你需要转换的内容放在括号里,像这样:int(转换的内容)。
1 2 3 4 5 6 7 |
number1 = '6' number2 = '1' print(int(number1)+int(number2)) #输出结果: 7 |
注意:只有符合整数规范的字符串类数据,才能被int()强制转换。
首先,整数形式的字符串比如’6’和’1’,可以被int()函数强制转换。
其次,文字形式,比如中文、火星文或者标点符号,不可以被int()函数强制转换。
最后,小数形式的字符串,由于Python的语法规则,也不能使用int()函数强制转换。
错误代码:
1 2 3 |
print(int('3.8')) # 运行后显示结果:ValueError:invalid literal for int() with base 10: '3.8' |
虽然浮点形式的字符串,不能使用int()函数。但浮点数是可以被int()函数强制转换的。
1 2 3 4 |
print(int(3.8)) #输出结果: 3 |
int()函数的本质是将数据转换为整数。所以对于浮点数,int()函数会做取整处理。但是,同我们平时对小数四舍五入的处理方法不同,int()函数会直接抹零,直接输出整数部分。
float()函数

将需要转换的数据放在括号里,像这样:float(数据)。
float()函数也可以将整数和字符串转换为浮点类型。同时,如果括号里面的数据是字符串类型,那这个数据一定得是数字形式。
1 2 3 4 5 6 7 8 9 10 11 12 |
height = 188.0 weight = 180.0 age = '89' print(height) print(weight) print(float(age)) #输出结果: 188.0 180.0 89.0 |
在上题中,height与weight变量已经是浮点类型,age是字符串类型(可以用type(变量)来查看数据类型),因此打印前对将age进行float(age)
转化即可。
总结:

大总结:

us pharmacy viagra – neurontin for sale oral neurontin 800mg
tadalafil without prescription – generic tadalafil sildenafil pills
lopressor 100mg ca – buy generic atenolol brand tadalafil 10mg
amlodipine 10mg for sale – order amlodipine online cheap brand prilosec 10mg
order generic metformin 500mg – order lipitor 10mg for sale atorvastatin 20mg ca
ivermectin – buy ivermectin 3 mg online azithromycin 500mg generic
cialis 10mg pill – order cialis generic tadalafil oral
order amoxil 250mg for sale – brand amoxil 500mg sildenafil pills 50mg
prednisone 5mg sale – order prednisone 10mg pills accutane tablet
red dog casino – free online slots order prednisone 20mg generic
purchase cialis without prescription – cost of ivermectin lotion ed pills comparison
tadalafil 10mg oral – cialis pills ivermectin 8 mg
order viagra for sale – viagra sildenafil 200mg order ranitidine 150mg without prescription
careprost pills – order desyrel 100mg sale buy desyrel 50mg pills
order rhinocort generic – generic ceftin 500mg buy cefuroxime 500mg sale
sildenafil online buy – brand nolvadex antabuse medication
cephalexin cheap – cleocin 300mg canada erythromycin 500mg cost
ivermectin brand – ivermectin 6 mg for people stromectol sales
plaquenil 400mg price – oral plaquenil order plaquenil pills
tadalafil 5mg drug – buy cialis 5mg online cheap cialis 20mg cheap