Important thing

1 '''
2     bit_length
3 1   0000 0001   1
4 2   0000 0010   2
5 3   0000 0011   2
6 '''

# 类型转化注意点

1 # int 转 str 无限制
2 # str 转 int 只有字符串为数字的时候可以转
3 # int 转 bool 只要是非零就是True else 为False
4 # bool 转 int True为 1  False 为 0
 
1 # str 转 bool  ''为False else 为True
2 # if s:
3 #    print("你输入的为空,请重新输入")
4 #   可以判断s 是否为非空字符
 

# 效率问题的选择
1 '''
2 while True: 的效率低于
3 while 1:    因为少了转化
4 '''
 

# 字符串全部打印

 
# s7 = s[:]
# s8 = s[0:]
# print(s7, s8)
 

# 倒叙打印

1 倒叙打印
2 s = 'ABCDLSESRF'
3 s14 = s[-1::-1]
4 print(s14)
# 每个隔开(特殊字符或者数字)的单词首字母大写
 
1 # 每个隔开(特殊字符或者数字)的单词首字母大写
2 s = 'alex*egon-wusir'
3 print(s.title())
4 
5 s = 'fade,crazy*w4rri0r_songsong node_3'
6 s4 = s.title()
7 print(s4)
 

# 测量字符串的元素个数 用len

 
# 以什么开头 startswith 以什么结束 endswiith
s = 'rainmZc'
print(s.startswith('rai'))
print(s.endswith("r", 3, 6))

# 可以利用此功能来筛选符合条件的字符串
if s.startswith('rainm'):
    print("handsome")
else:
    pass
# find 通过元素找索引,找不到返回-1
# index通过元素找索引,找不到会报错

# 逐个输出字符串元素
1 # 逐个输出字符串元素
2 s = "123dsf g123..官方给的412s"
3 for i in s:
4     print(i)
5 name = input("请输入名字:")
6 if 'sb'.upper() in name.upper():
7     print("你才是{}".format(name))
8 else:
9     print(1111)
 
# format的三种用法
1 name = "{} is very {} and {}".format('Rainm', 'handsome', 'handsome')
2 print(name)
3 # 按顺序输出
4 name = "{1} is very {0} and {0}".format("handsome", "Rainm")
5 print(name)
6 name = "{name} is very {a} and {a}".format(a="handsome", name="Rainm")
7 print(name)
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/Rainm/p/9589995.html
今日推荐