6.Python字符串
In [1]:
s1 = 'abc'
s2 = "abc"
s3 = '''
abc
''' #三引号占用的内存空间与单双引号不同,前提是三引号的内容不在一行上,包含了换行。
print(id(s1),id(s2),id(s3))
print(s1, s2, s3)
In [2]:
s1 = input('Please input string:')
s2 = input('Please input string:')
print(s1 == s2)
print(s1 is s2) #常量is是true,input输入底层做了处理,因此最后输出的地址是不一样的
In [3]:
#例1:
s3 = s1 + s2 #相当于把s1和s2拼接
print(s3)
In [4]:
#例2:
s4 = s1 * 5 #相当于把s1复制5次拼接
print(s4)
In [5]:
#例3:
s5 = 'stenen'
result = 'ste' in s5
print(result)
In [6]:
#例4:
s5 = 'stenen'
result = 'ste' not in s5
print(result)
In [7]:
#例5:
print('%s说:%s' %('老师','大家要好好学习'))
print('%s说:\'哈哈哈\'' %('steven'))
print(r'%s说:\'哈哈哈\'' %('steven')) #加 r 可以保留转义字符不转义
In [8]:
filename = 'picture.png'
print('------------默认顺序数------------')
print(filename[6]) #取第6个字符,下标为5的字符
print(filename[0:6]) #取下标为0-5个字符
print(filename[3:]) #冒号后面省略表示一直取到结尾
print(filename[:3]) #冒号前省略表示从第0个开始
print('--------------倒着数--------------')
print(filename[-1]) #取倒数最后一个字符
print(filename[-1:])
print(filename[-4:])
print('---------------倒叙---------------')
print(filename[::-1])
print(filename[::])
In [9]:
message = 'zhaorui is a beautiful girl!'
In [10]:
msg = message.capitalize()
print(msg)
In [11]:
msg = message.title()
print(msg)
In [12]:
msg = message.upper()
print(msg)
In [13]:
msg = message.lower()
print(msg)
In [14]:
#案例:验证码
s = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789'
print(len(s))
code = ''
import random
for i in range(4):
ran = random.randint(0, len(s)-1)
code += s[ran]
print('验证码:', code)
user_input = input('请输入验证码:')
if code.lower() == user_input.lower():
print('验证码正确')
else:
print('验证码输入错误')
In [15]:
s1 = 'index lucy lucky goods'
#s = '01234 6789 1112131415 1718192021'
In [16]:
result = 'R' in s1
print(result)
In [17]:
position = s1.find('R')
print(position)
position = s1.find('u')
print(position)
p2 = s1.find('u', position+1, len(s1))
print(p2)
In [18]:
#例2:从博客链接找博客名
url = 'https://www.cnblogs.com/King-Penguin'
p = url.rfind('/')
print(url[p+1:])
In [19]:
#例3:将链接中的'/'替换为'#'
url = 'https://www.baidu.com'
new_url = url.replace('/', '#')
print(new_url)
In [20]:
msg = '好好学习和天天向上'
result = msg.encode('utf-8')#gbk:中文 gb2312:简体中文 unicode
print(result)
In [21]:
result = b'\xe5\xa5\xbd\xe5\xa5\xbd\xe5\xad\xa6\xe4\xb9\xa0\xe5\x92\x8c\xe5\xa4\xa9\xe5\xa4\xa9\xe5\x90\x91\xe4\xb8\x8a'
str = result.decode('utf-8')
print(str)
In [22]:
#案例:文件上传并只能上传以'He'命名开头的图片
filename1 = '笔记.doc'
filename2 = 'Hello.png'
result = filename1.endswith('.png') or filename1.endswith('.jpg') #判断文件是否以'.png'或'.jpg'结尾
if result:
print('正准备上传filename1...')
else:
print('filename1不满足上传条件')
result = filename2.endswith('.png') or filename1.endswith('.jpg') #判断文件是否以'.png'或'.jpg'结尾
if result:
print('正准备上传filename2...')
else:
print('filename2不满足上传条件')
In [23]:
str1 = 'abcdef123'
str2 = 'abcdef'
str3 = '123456'
print(str1.isalnum())
print(str1.isalpha())
print(str1.isdigit())
print('-------------------')
print(str2.isalnum())
print(str2.isalpha())
print(str2.isdigit())
print('-------------------')
print(str3.isalnum())
print(str3.isalpha())
print(str3.isdigit())
In [24]:
new_str = '-'.join('abc')
print(new_str)
list = ['a', 'b', 'c']
new_list = ' '.join(list)
print(new_list)
In [25]:
url = 'https://www.baidu.com'
print('url中"."的个数共有:', url.count('.'))
版权所有
http://www.cnblogs.com/King-Penguin
posted on 2019-12-26 14:26 King-Penguin 阅读(601) 评论(0) 编辑 收藏 举报