码迷,mamicode.com
首页 > 编程语言 > 详细

Python笔记004——字符串

时间:2015-11-13 00:45:25      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:

字符串基本定义

1 字符串用引号引起来的(单引号,双引号,三双引号,三单引号)

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

2 斜杠表转义(\",输出 “)

3 \b的含义

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

字符串运算

加法运算(拼接)

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

乘法运算

1 a=wu
2 b=jia
3 c=dong
4 print(a*3)
wuwuwu

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

1 a=wu
2 b=jia
3 c=dong
4 if a>b:
5    print(a)
6 else:
7   print(b)
[root@localhost ~]# python 1.py
wu

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

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

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

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

统计字符串个数及其位置

1 a=wujiadong*17
2 b=j
3 print(a.count(b))
oot@localhost ~]# python 1.py
17 
1 a=wujiadong*17
2 b=j
3 #print(a.count(b))
4 i=0
5 while i< len(a):
6    if a[i]==b:
7       print(i,a[i])
8    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)

 

字符串的应用

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

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

 

1 import random
2 i=1
3 while i<=10:
4    x=random.randint(100000,1000000)
5    s=str(x)
6    if 4in s and 7  in s:
7       print(s,has 4 and 7)
8    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(都有)

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

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

 1 import random
 2 i=1
 3 while i<=10:
 4    x=random.randint(100000,1000000)
 5    s=str(x)
 6    if 4 in s or 7  in s:
 7        print(s)
 8        k=0
 9        while k < len(s):
10           if s[k]==4or s[k]==7:
11              print(k,s[k])
12           k +=1
13    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

1 i=0
2 while i <=100:
3    if i<10:
4       print(jeapedu00+str(i))
5    elif i<100:
6       print(jeapedu0+str(i))
7    else:
8       print(jeapedu+str(i))
9    i +=1

例:构造a01b02c03.....y25z26

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

1 a=chr(65)
2 print(a)
3 a=chr(97)
4 print(a)
5 
6 b=ord(a)
7 print(b)
8 b=ord(A)
9 print(b) 
1 a=97
2 i=0
3 while i< 26:
4 #   print(chr(a+i))
5     print(chr(a+i)+str(i))
6     i +=1

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

1 a= hello jeapedu.com
2 sub=jeap
3 i=0
4 while i<len(a):
5    if a[i]==sub[0]and a[i+1]==sub[1]and a[i+2]==sub[2]and a[i+3]==sub[3]:
6       print(i,a[i]+a[i+1]+a[i+2]+a[i+3])
7    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’

1 s=welcome to the cruel world
2 t=welcome to 
3 i=11
4 while i <len(s):
5    t +=s[i]
6    i +=1
7 print(t)
[root@localhost ~]# python 1.py
welcome to the cruel world

切片

1 a=hello the cruel world
2 print(len(a))
3 print(a[0])
4 print(a[3:5])
5 print(a[3:])
6 print(a[:21])
7 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’

1 a=hello the cruel world
2 sub=rue
3 i=0
4 while i<len(a)-len(sub)+1:
5    if a[i:i+3]==sub:
6       print(yes,i)
7    i +=1
[root@localhost ~]# python 1.py
(yes, 11)

 

Python笔记004——字符串

标签:

原文地址:http://www.cnblogs.com/wujiadong2014/p/4937873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!