切片:
s="shdfksd"
print(s) 打印shdfksd
print(:) 打印shdfksd
print(s[0]) 打印s
print(s[1:4:2])打印hk
print(s[::-1]) 打印dskfdha
print(s[:-1]) 打印shdfks
拼接:
print('hello'+'world') 打印hello world
print("h" in s) 打印True
判断
print('123'.isdigit()) 判断字符串是否为纯数字
print('Hello'.istitle())判断字符串是否为标题(首字母大写)
print('hello'.upper()) 大写输出
print('HELLO'.lower()) 小写输出
print('hello'.isupper())判断是否为大写
print('HELLO'.islower())判断是否为小写
print('123'.isalpha()) 判断是否为字母
判断开头结尾:
url1="http://172.25.254.250/index.html"
url2="ftp:///172.25.254.250/pub"
url3="file://mnt"
filename=login.php
if usr3.startswith('http://'):
print("ok")
elif filename.endswith('.php'):
print("yes")
去除字符串中特定的字符
s.strip() 去除s字符串空格,回车,制表符
s.lstrip() 去除左边的空格
s.rstrip() 去除右边的空格
s.strip('a') 去掉里边字符串里边的a
s.lstrip('a') 去掉左边字符串里边的a
s.rstrip('a') 去掉右边字符串里边的a
查找
s.find('hello')找到s中的子串并返回最小索引
s.rfind('hello')找到s中的子串并返回最大索引
替换
s.replace('hello','westos') 把hello替换成westos
格式打印
print('ss'.center(20))总共打印20个字符,ss放在最中间
print('ss'.center(20,'*'))总共打印20个字符,ss放在最中间,其他的使用*填充
print('ss'.ljust(20,'*'))总共打印20个字符,ss放在最左边,其他使用*代替
print('ss'.rjust(20,'*'))总共打印20个字符,ss放在最右边,其他使用*代替
计数
print('hello'.count('l'))统计hello中l的个数
分割
s='172.25.254.250'
print(s.split('.')) 将s字符串以'.'分割成列表
print(s.split('.')[::-1])以'.'分割倒叙输出
拼接
date=['2019','06','26']
print('-'.join(date)) 字符串连接,以'.'连接