2-1 简单消息:将一条消息存储到变量中,再将其打印出来。
str="bigelow"
print (str)
2-2 多条简单消息:将一条消息存储到变量中,将其打印出来;再将变量的值修改为一条新消息,并将其打印出来。
str="bigelow"
print(str)
str="change"
print(str)
2-3 将用户姓名存在一个变量,并向该用户显示一条信息。
name=input()
str="Hello "+name+", would you like to learn some Python today?"
print(str)
2-4 调整名字的大小写:将一个人名字存储到一个变量中,再以小写、大写和首字母大写的方式显示这个人命
name=input()
print(name.title()) #首字母大写
print(name.upper()) #大写
print(name.lower()) #小写
2-5 名言:打出一句名人名言
print('Maxim Gorky said, "When work is a pleasurem, life is joy! When work is duty, life is slavery."')
2-6 名言2:
famous_people="Maxim Gorky"
famous_words="'When work is a pleasure, life is joy! When work is duty, life is slavery.'"
sum=famous_people+" said, "+famous_words
print(sum)
2-7 删除人命中的空白:存储一个人命,删除左右的空白
word=" \thello world\n"
print(word.strip()) #显示"hello world"
print(word.rstrip()) #显示" hello world"
print(word.lstrip()) #显示"hello world↓"
2-8 输出4个表达式,它们的结果都是8
print(3+5)
print(10-2)
print(1*8)
print(int(16/2)) #python3中整数相除也会显示浮点数,所以要用Int
2-9 选择一个数字,放入字符串中,并找到其位置
found=2
str1="blaaaaaaa"+str(found)+"blilililili"
import re #此处引入正则表达式式模块,其中的函数可以方便地查找到目标字符的位置
re.search(str(found),str1).start() #start为search函数中的子函数,可以显示查找到的字符串的首地址
结果:9
2-10 添加注释
单行注释:#
多行注释:''' 注释 '''
2-11 python之禅
import this 召唤神龙!