Python入门之字符串及其方法详解

1 字符串的定义

a = 'hello'
b = "westos"
c = 'let\'s go'       #两个单引号之前第三个引号必须转义 
d = "let's go"
e = """
        用户管理系统
        1.添加用户
        2.删除用户
        3.显示用户
"""
print(a)
print(b)
print(c)
print(d)
print(e)
print(type(e))

测试结果:
hello
westos
let's go
let's go

        用户管理系统
        1.添加用户
        2.删除用户
        3.显示用户

<class 'str'>

2 字符串的特性

索引:
<python中字符串中的字符是通过索引来提取的,索引从0开始,
同样也可从末尾提取,最后一个字符为-1>

索引
s = 'hello'
print(s[0])
print(s[1])
print(s[-1])

输出结果:
h
e
o

切片:同时提取字符串中的若干个字符

 #切片
print(s[:3])    #显示前3个字符
print(s[1:3])   #显示1到3字符
print(s[0:4:2]) #s[start:end:step] 从start开始,到end-1结束
print(s[:])     #显示所有字符
print(s[::-1])  #对字符串倒叙输出
print(s[1:])    #除了第一个字符以外,其他全部显示除了第一个字符以外,其他全部显示
print(s[:-1])    #从最后一个字符开始倒叙输出

重复

#重复
print(s * 5)

s = 'python'
print(s*5)

测试结果:
pythonpythonpythonpythonpython

连接

#连接
print('hello' + 'world')

成员操作符:in或not in

#成员操作符号
print('h' in s)
print('q' in s)
 s = 'python'
 print('h' in s)
 print('t' not in s)
    
 测试结果:
 Ture
 Fales
    
    
 for循环(迭代)
 s = 'python'
 for i in s:
     print(i)
    
测试结果:
p
y
t
h
o
n

3 字符串处理

字符串判断是否是数字:isdigit
判断字符串中每个元素为指定类型,其中只要有一个不满足就返回False

print('123'.isdigit())
print('123abc'.isdigit())
print('123C'.isdigit())

测试结果:
True
False
False

字符串判断某个字符串是否为标题(第一个字母大写,其余字母小写):istitle

print('Hello'.istitle())
print('HeLlo'.istitle())
    
测试结果:
True
False

字符串的转换及判断大小写:upper(大写)和lower(小写)

print('hello'.upper())
print('heLLo'.lower())
print('hello'.isupper())
print('hello'.islower())
输出结果:
HELLO
hello
False
True

字符串是否由字母和数字及只由字母组成:isalnum 和isalpha

print('hello123'.isalnum())
print('123'.isalpha())
print('aaa'.isalpha())

测试结果:
True
False
True

字符串开头和结尾的匹配:startswith(开头),endwith(结尾)
 (1).匹配结尾

filename = 'hello.log'
#如果filename以.log结尾打印filename,否则打印error
if filename.endswith('.log'):
    print(filename)
else:
    print('error filename')

测试结果:
hello.log

(2).匹配开头

url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub'
url3 = 'http://172.25.254.250/index.html'

if url3.startswith('http://'):
    print('爬取该网页')
else:
    print('错误网页')

测试结果:
爬取该网页

字符串去除两边空格:lstrip()左边空格,rstrip()右边空格
 (1).去除空格

s = '    hello    '
#\t也表示空白字符相当于一个Tab键四个空格
s1 = '\thello\t'

print(s)
print(s.strip())
print(s.rstrip())
print(s.lstrip())
print(s1)
print(s.rstrip())
print(s.lstrip())

测试结果:
    hello    
hello
    hello
hello    
    hello	
    hello
hello  

(2).去除指定字符:strip(‘指定字符’)

s = 'helloh1'
print(s.strip('h'))     #去除字符串中的h字符
print(s.lstrip('he'))   #去除最左边的he字符
print(s.rstrip('h1'))   #去除最右边的h1字符
print(s.rstrip('o'))    #因为最右边没有o字符所以字符串不变

测试结果:
elloh1
lloh1
hello
helloh1

字符串的搜索(find,rfind)与替换(replace)
 (1).find:找到子串,并返回最小的索引
 (2).rfind:找到子串,并返回最大索引

s = 'hello world hello'

print(s.find('hello'))
print(s.find('world'))
print(s.rfind('hello'))

测试结果:
0
6
12

(3).replace:替换字符串中指定字符

s = 'hello world hello'

#替换字符串中所有的’hello‘为’westos‘
print(s.replace('hello','westos'))

测试结果:
westos world westos

字符串的对齐:center

print('学生管理系统'.center(30))
print('学生管理系统'.center(30,'*'))
print('学生管理系统'.center(30,'@'))
print('学生管理系统'.ljust(30,'*'))
print('学生管理系统'.rjust(30,'*'))

测试结果:
            学生管理系统            
************学生管理系统************
@@@@@@@@@@@@学生管理系统@@@@@@@@@@@@
学生管理系统************************
************************学生管理系统

字符串的统计:count
注:len是用来统计字符串长度的

print('hello'.count('l'))
print('hello'.count('ll'))
print(len('hello'))

测试结果:
2
1
5

字符串的分离和连接:split(分离),join(连接)
 (1).分离:split

s = '172.25.254.78'
s1 = s.split('.')   #以.号为分隔符分离字符串s,默认为列表形式
print(s1)
print(s1[::-1])     #以.号为分隔符倒序输出
print(s[::-1])
date = '2019-03-26'
date1 = date.split('-')
print(date1)

测试结果:
['172', '25', '254', '78']
['78', '254', '25', '172']
87.452.52.271
['2019', '03', '26']

(2).连接:join

date = '2019-03-26'
date1 = date.split('-')
print(date1)
#通过指定的字符进行链接
print(''.join(date1))
print('/'.join(date1))

测试结果:
['2019', '03', '26']
20190326
2019/03/26

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值