字符串的定义
字符串的定义有三种方式:
•第一种方式:
str1 = ‘our company is westos’
•第二种方式:
str2 = “our company is westos”
•第三种方式:
str3 = “”“our company is westos”“”
==第三种方式表示块注释:多行代码注释 ==
转义符号
一个反斜线加一个单一字符可以表示一个特殊字符,通常是不可打印的字符
\n: 代表换行符 \”: 代表双引号本身
\t: 代表tab符 \’: 代表单引号本身
字符串的特性
##索引( s[num] )——0 1 2 3 4 索引从0开始
以hellow为例
s = ‘hello’
print(s[0])
print(s[1])
##拿出最后一个字符
s = ‘hello’
print(s[4])
print(s[-1])
##索引加步长s[start:stop:step]
从satrt开始到end -1结束,步长为step
print(s[0:3])
print(s[0:4:2])
##显示所有的字符
print(s[:])
##显示前3个字符
print(s[:3])
##字符串的反转
print(s[::-1])
##除了第一个字符之外的其他全部字符
print(s[1:])
##重复
print(s * 10)
##连接
print('hello ’ + ‘python’)
成员操作符
print(‘he’ in s)
print(‘aa’ in s)
print(‘he’ not in s)
for循环遍历
for i in s:
print(i)
字符串的常用方法
检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False——.istitle()
检测字符串中所有的字母是否都为大写,如果全部大写则返回Ture,否则返回 False——isupper()
检测字符串是否由小写字母组成,如果全部小写则返回Ture,否则返回 False——.islower()
转换字符串中所有大写字符为小写——lower()
转换字符串中所有小写字符为大写——upper()
转换字符串为标题模式(首字母大写)——.title()
截掉字符串左边的空格或指定字符——.lstrip()
截掉字符串右边的空格或指定字符——.rstrip()
截掉字符串中间或指定字符——strip()
##检测字符串中所有的单词拼写首字母是否为大写
>>> 'hello'.istitle()
False
##检测字符串中所有的字母是否都为大写
>>> 'hello'.isupper()
False
>>> 'Hello'.isupper()
False
>>> 'HELLO'.isupper()
True
##检测字符串是否由小写字母组成
>>> 'hello'.islower()
True
>>> 'Hello'.islower()
False
##转换字符串中所有大写字符为小写
>>> 'Hello'.lower()
'hello'
>>> a = 'Hello'.lower()
>>> a
'hello'
##转换字符串中所有小写字符为大写
>>> a = 'Hello'.upper()
>>> a
'HELLO'
##转换字符串为标题模式(首字母大写)
>>> a = 'Hello'.title()
>>> a
'Hello'
>>> s = ' hello'
>>> s
'\thello'
>>> s = ' hello'
>>> s
' hello'
##截掉字符串左边的空格或指定字符
>>> s.lstrip()
'hello'
>>> s = ' hello '
>>> s
' hello '
>>> s.lstrip()
'hello '
##截掉字符串右边的空格或指定字符
>>> s.rstrip()
' hello'
>>> s.strip()
'hello'
>>> s = ' hello'
>>> s
'\thello'
>>> s.strip()
'hello'
>>> s = ' hello\n'
>>> s
'\thello\n'
##截掉字符串中的空格或指定字符
>>> s.strip()
'hello'
>>> s = 'helloh'
>>> s.strip('h')
'ello'
>>> s.lstrip('h')
'elloh'
测试字符串是否以指定的后缀结束——.endswith
filename = 'hello.loggg'
if filename.endswith('.log'):
print(filename)
else:
print('error.file')
测试字符串是否以指定的前缀开始—— .startswith
url = 'https://172.25.254.250/index.html'
if url.startswith('http://'):
print('爬取内容~~')
else:
print('不能爬取~~')
字符串的判断
检测字符串是否只由数字组成——.isdigit()
print(‘asdq3w4e’.isalpha())
检测字符串是否只由字母组成——.isalpha()
print(‘23456ertyfg’.isalpha())
检测字符串是否由字母和数字组成——.isalnum()
print(‘weewdsfd3242’.isalnum())
字符串的对齐
字符串居中,默认填充字符为空格——str.center(width[, fillchar]) width – 字符串的总宽度; fillchar – 填充字符。
print(‘学生管理系统’.center(30))
原字符串左对齐,默认填充字符为空格——str.ljust(width[, fillchar]) width – 字符串的总宽度; fillchar – 填充字符。
print(‘学生管理系统’.ljust(30))
原字符串右对齐,默认填充字符为空格——str.rjust(width[, fillchar]) width – 字符串的总宽度; fillchar – 填充字符。
print(‘学生管理系统’.rjust(30))
字符串的统计
统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置
str.count(sub, start= 0,end=len(string))
sub -- 搜索的子字符串
start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
print(‘hello’.count(‘l’))
print(‘hello’.count(‘ll’))
print(len(‘wfqfqfqfq’))
字符串的分离和连接
通过指定分隔符对字符串进行切片——split()
s = '172.25.254.70'
s1 = s.split('.')
print(s1)
print(s1[::-1])
date = '2019-9-1'
date1 = date.split('-')
print(date1)
通过指定的连接符号,连接每个字符——.join
date = '2019-9-1'
date1 = date.split('-')
print(date1)
print(''.join(date1))
print('/'.join(date1))
print('~~'.join('hello'))