Python学习之路Day2

xiaoxiao2021-02-27  131

**

1.变量的使用与赋值

**

message = "world!" print(message) #输出结果为:world message = "hello world!" #输出结果为:hello world print(message)

**

2.字符串

**用引号(单/双引号)括起来的都是字符串

str1 = "This is a string" str2 = 'this is also a string'

2.1使用方法修改字符串的大小写

message = "hello worLd" print(message.title()) #title()方法,以首字母大写的方式显示每个单词 print(message.upper()) #upper()方法,以所有字母大写的方式显示每个单词 print(message.lower()) #lower()方法,以所有字母小写的方式显示每个单词

2.2合并字符串

first_name = "Zhang" second_name = "San" full_name = first_name + " "+second_name print(full_name) #打印结果为:Zhang San

2.2去除字符串首尾的空白(空格)

name = " Zhang San " print(name) print(name.rstrip()) #rstrip()删除字符串末尾的空格 print(name.lstrip()) #lstrip()删除字符串首部的空格 print(name.strip()) #strip()删除字符串首尾的空格

3.数字

python使用两个乘号表示乘方运算

print(4+4) print(9-1) print(16/2) print(4*2) print(2**3) #上述语句打印出来均为8,第三个,打印出来是8.0 age = 23 #python中要使数字与其他连接时,需强制转换str(age)将其转换为字符串类 #型,否则报错 message = "Happy" + str(age) + "rd Brithday" print(message)
转载请注明原文地址: https://www.6miu.com/read-15790.html

最新回复(0)