Python-字符串

浏览: 1004

字符串基础

1 字符串定义:用引号引起来的字符集合称之为字符串(单引号,双引号,三双引号,三单引号)

    三引号(三单或三双)支持多行或也可表示注释,而单双引只能一行(但是可以加\n)

2 转义字符串:

常见有 \n 回车字符,

         \t 下一制表位

         \'' 双引号

         \' 单引号

         \\ 输出斜杠

         \b 往前退格(删除前面那个字符)

 print('hello world')
print('hello world\bwujiadong')#删除前一个字符,后面字符往前进一个
[root@localhost ~]# python 1.py
hello world
hello worlwujiadong

3 原字符串,若不要被看成是转义字符,那么需在字符串前加上r,称此时的字符串为原字符串

字符串运算

加法运算(拼接)

 a='wu'
b='jia'
c='dong'
print(a+b+c)
[root@localhost ~]# python 1.py
wujiadong

乘法运算(重复)

 a='wu'
b='jia'
c='dong'
print(a*3)
wuwuwu

关系运算(比较:对应字符相比)

 a='wu'
b='jia'
c='dong'
if a>b:
print(a)
else:
print(b)
[root@localhost ~]# python 1.py
wu

in 运算与not in运算 (a in b 表示判断a是不是在b里面)

 a='wujiadong'
b='wu'
if 'wu' in a: #or b ,not wu
print('true')
else:
print('false')
[root@localhost ~]# python 1.py
true

字符串访问(index索引【0到len()-1】,slice)

 a='wujiadong'# 0,1,2,3,4,5,6,7,8
print(a[0])
print(a[8])
print(a[0]+a[8])
i=0
while i<len(a):
print(a[i])
i +=1
[root@localhost ~]# python 1.py
w
g
wg
w
u
j
i
a
d
o
n
g
 a='wujiadong'# 0,1,2,3,4,5,6,7,8
i=0
while i< len(a): #为什么这里不能 <=
if a[i]=='d': #遍历的方式找一个字符
print(a[i],i)
i=i+1
[root@localhost ~]# python 1.py
(
'd', 5)

slice切片:str_name[start:end:step]

统计字符串个数及其位置

 a='wujiadong'*17
b='j'
print(a.count(b))
oot@localhost ~]# python 1.py
17
 a='wujiadong'*17
b='j'
#print(a.count(b))
i=0
while i< len(a):
if a[i]==b:
print(i,a[i])
i +=1
[root@localhost ~]# python 1.py
(
2, 'j')
(
11, 'j')
(
20, 'j')
(
29, 'j')
(
38, 'j')
(
47, 'j')
(
56, 'j')
(
65, 'j')
(
74, 'j')
(
83, 'j')
(
92, 'j')
(
101, 'j')
(
110, 'j')
(
119, 'j')
(
128, 'j')
(
137, 'j')
(
146, 'j')

 字符串函数【help(str)】

常见函数有find,index,replace,count,split,strip

isspace()

 s=' wujiadong '
i=0
while i<len(s):
if s[i].isspace(): #
print(i)
i += 1
[root@localhost ~]# python 1.py
0
10

 find()和rfind()

 sub='11aa22cc33aa'
a=sub.find('aa',0,12) #找到的第一个符合字符的index
b=sub.rfind('aa',0,12) #找到最后一个符合的字符的index
print(a,b)
[root@localhost ~]# python 1.py
(
2, 10)

基于前一次位置往后找

 s= 'hello jeapedu.com'*4
sub='eape'
i=0
pos=-4 #pos= -len(sub)
while i'eape'):
pos=s.find(sub,pos+len(sub))
print(pos)
i += 1
[root@localhost ~]# python 1.py
7
24
41
58

例:

s='http://www.baidu.com http://www.hao123.com http://www.sohu.com http://www.yahu.com'
h='http'
c='.com'
posh=-len(h)
posc=-len(c)
i=0
while i<s.count(h):
posh= s.find(h,posh+len(h))#posh= s.find(h,posc+len(h))
posc= s.find(c,posh+len(c))#posc= s.find(c,posh+len(c))
print(posh),
print(posc),
print(s[posh:posc+len(c)])
i += 1
[root@localhost ~]# python 1.py
0 16 http://www.baidu.com
21 38 http://www.hao123.com
43 58 http://www.sohu.com
63 78 http://www.yahu.com

去掉字符串s前后的空格

 s=' wujiadong '
i=0
h=-1
e=-1
while i<len(s):
if not s[i].isspace():
h=i
break
i += 1
print(h)
i=len(s)-1
while i>=0: # 从最后一个往第一个遍历
if not s[i].isspace():
e=i
break
i -=1
print(e)
print(s[h:e+1])
[root@localhost ~]# python 1.py
1
9
wujiadong

strip函数

 a='  wujiadong  '
t="|"+a+'|'
print('1',t)
t1='|'+a.strip()+'|'
print('2',t1)
b='ab123456ab'
t1=b.strip('ab')
print('3',t1)
t2=b.strip('a')
print('4',t2)
b2='123xxx321'
t3=b2.strip('321')
print('5',t3)
[root@localhost ~]# python 1.py
(
'1', '| wujiadong |')
(
'2', '|wujiadong|')
(
'3', '123456')
(
'4', 'b123456ab')
(
'5', 'xxx')

例:

将字符串s中的网址提出来

 s='*&)%$#(@http://www.baidu.com*&^)^(%$#'
a='*&^)(%$#@'
i=0
h=0
while i<len(s):
if s[i] not in a:
h=i
break
i +=1
s=s[i:]
print(s)
i=len(s)-1
e=0
while i>=0:
if s[i] not in a:
e=i+1
break
i -=1
s=s[:e]
print(s)
[root@localhost ~]# python 1.py
http:
//www.baidu.com*&^)^(%$#
http://www.baidu.com

字符串的应用

例:用random.randint随机产生一个六位数

     1)判断此数里是否含有4和7(都有)

 import random
i=1
while i<=10:
x=random.randint(100000,1000000)
s=str(x)
if '4'in s and '7' in s:
print(s,'has 4 and 7')
i +=1
[root@localhost ~]# python 1.py
(
'774596', 'has 4 and 7')
(
'464273', 'has 4 and 7')
(
'457881', 'has 4 and 7')
(
'330487', 'has 4 and 7')

      2)在没有4和7的情况下,有没有6和8(都有)

import random
i=1
while i<=10:
x=random.randint(100000,1000000)
s=str(x)
if '4'not in s and '7' not in s:
#print(s,'has 4 and 7')
if '6' in s and '8' in s:
print(s,'no 4and7,but have 6and8')
i +=1
[root@localhost ~]# python 1.py
(
'181196', 'no 4and7,but have 6and8')
(
'858986', 'no 4and7,but have 6and8')

     3)若有4和7出现,打印其出现的位置

 import random
i=1
while i<=10:
x=random.randint(100000,1000000)
s=str(x)
if '4' in s or '7' in s:
print(s)
k=0
while k < len(s):
if s[k]=='4'or s[k]=='7':
print(k,s[k])
k +=1
i +=1
[root@localhost ~]# python 1.py
379467
(
1, '7')
(
3, '4')
(
5, '7')
950484
(
3, '4')
(
5, '4')
865870
(
4, '7')
357986
(
2, '7')
745119
(
0, '7')
(
1, '4')
267922
(
2, '7')
555634
(
5, '4')
429089
(
0, '4')

 例:打印jeapedu000-jeapedu100

i=0
while i <=100:
if i<10:
print('jeapedu00'+str(i))
elif i<100:
print('jeapedu0'+str(i))
else:
i +=1

例:构造a01b02c03.....y25z26

利用chr(ascii转字符)ord(字符转ascii)int(字符串转整型)实现翻译

 a=chr(65)
print(a)
a=chr(97)
print(a)

b=ord('a')
print(b)
b=ord('A')
print(b)
 a=97
i=0
while i< 26:
# print(chr(a+i))
print(chr(a+i)+str(i))
i +=1

如何判断字符串里有‘jeap’这个子字符串?其位置?

 a= 'hello jeapedu.com'
sub='jeap'
i=0
while i<len(a):
if a[i]==sub[0]and a[i+1]==sub[1]and a[i+2]==sub[2]and a[i+3]==sub[3]:
print(i,a[i]+a[i+1]+a[i+2]+a[i+3])
i +=1
[root@localhost ~]# python 1.py
(
6, 'jeap')

切片:stringname[start:end]

例:s='welcome to the cruel world'

      t='welcome to ' 将组成新字符串 t=‘welcome to the cruel world’

 s='welcome to the cruel world'
t='welcome to '
while i <len(s):
t +=s[i]
i +=1
print(t)
[root@localhost ~]# python 1.py
welcome to the cruel world

切片

 a='hello the cruel world'
print(len(a))
print(a[0])
print(a[3:5])
print(a[3:])
print(a[:21])
print(a[:])
[root@localhost ~]# python 1.py
21
h
lo
lo the cruel world
hello the cruel world
hello the cruel world

例:a=‘hello the cruel world’,看字符串a中是否有子串sub=‘rue’

 a='hello the cruel world'
sub='rue'
i=0
while i1:
if a[i:i+3]==sub:
print('yes',i)
i +=1
[root@localhost ~]# python 1.py
(
'yes', 11)
推荐 0
本文由 邬家栋 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册