Python学习之路Day2

来源:互联网 发布:xperia x compact 知乎 编辑:程序博客网 时间:2024/05/16 09:54

**

1.变量的使用与赋值

**

message = "world!"print(message)              #输出结果为:worldmessage = "hello world!"    #输出结果为:hello worldprint(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_nameprint(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.0age = 23#python中要使数字与其他连接时,需强制转换str(age)将其转换为字符串类#型,否则报错message = "Happy" + str(age) + "rd Brithday"print(message)
原创粉丝点击