day1总结

print("hello world")
name='王维是傻屌'
print(name)
age_of_王维是傻屌 = 18




















# type:用于判断变量的类型
str1 ='hello'
print(type(str1))

# value
str2 ='hello'
print(type(str2))
'''
用户与程序交互
输入:
input()
输出:
print()
'''
#让用户输入用户名
name=input('请输入名字:')

#输出用户名
print(name)

print(type(name))

#双引号
str2="遇到米老鼠"
print(str2)
print(type(str2))

#三引号
str3='''
安徽省
合肥市
最牛的学院
合肥学院
'''
print(str3)
print(type(str3))

'''
优先掌握的操作
1按索引取值
2切片
3长度(len)
4成员运算in和notin
5溢出空白
6切分
7循环
'''
#1 按索引取值
#正向
str1='hello tank!'
print(str1[0])  #h
print(str1[9])  #h

#反取向
print(str1[-2])  #k

#2 切片(顾头不顾尾)
str1 ='hello tank!'
#0-(5-1)
print(str1[0:5])

#步长
print(str1[0:11])   #hello tank!
print(str1[0:11:2])   #hlotn!

#长度
print(len(str1))  #11

#4.成员运算in和not in
print('h' in str1)
print('h' not in str1)

#5.移除空白strip
# 会移除字符串中左右两边的空格
str1 = ' hello tank!'
print(str1)
str1 = '  hello tank!   '
print(str1)
print(str1.strip())


#去除指定字符
str2 = '!tank!'
print(str2.strip('!'))

#6 切分split
str1 = 'hello tank!'
#根据str1内的空格进行切分
#c切分出来的值会存放在[]列表中。
print(str1.split( ' '))  #['hello', 'tank!']

#7.循环
#对str1字符串进行遍历,打印每一个字符
for line in str1:
    print(line)

'''
七 格式化输出
#占位符:
%s:可以替换任意类型
%d:替换数字类型
'''
'''
字符串格式化
#把100替换给%s
#str1 = '尊敬的用户,你好!您好!您本月的话费扣除%s元,还剩0元'%100

'''
'''
字符串类型:
    需要掌握的
'''
#1.strip,lstrip,rstrip
str1 ='   hello wuyuefeng   '
print(str1)

#去掉两边空格
print(str1.strip())
#去掉左边空格
print(str1.lstrip())
#去掉右边空格
print(str1.rstrip())

#2 lower ,uppper
str1 = 'hello wuyuefeng'
#转换成小写
print(str1.lower())
#转换成大写
print(str1.upper())

#3 startswith ,endswith
str1 = 'hello shadiao'
#判断str1字符开头是否等于hello
print(str1.startswith('hello')) #ture
#判断str1字符末尾是否等于shadiao
print(str1.endswith('shadiao')) #ture

#4.format(格式化输出)的三种玩法
# str1 = 'my name is %s, my age %s!' % ('tank', 18)
#  ; print(str1)

#方式一:根据位置順序格式化
print('my name is {}, my age {}!'.format('tank', 18))
#方式二:根据索引格式化
print('my name is {O}, my age {1} !'. format('tank',18))
#方式三:指名道姓地格式化
print('my name is {name}, my age {age} !'. format(age=18, name=' tank' ))


#join 字符串拼接
#报错,只允许字符串拼接
#print(''.join('tank','18','form GZ'))
#根据空格,把列表中的每一个字符串进行拼接
print(''.join(['tank','18','from GZ']))
#根据_,把列表中的每一个字符串进行拼接
print('_'.join(['tank','18','from GZ']))


# 7. replace: 字符串替换
str1=  'my name  is  WangWei, my age 73!'
print(str1)
str2 = str1. replace (' WangWei','sb')
print (str2)

# 8、isdigit: 判断字符串是否是数字
choice = input(' 请选择功能[0,1, 2]:')
#判断用户输入的选择是否是数字
print (choice. isdigit())

猜你喜欢

转载自www.cnblogs.com/hxssb/p/11079805.html