VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > python教程 >
  • python基础教程之Python基础-列表、元组、字典、字符串(精简解析),全网最齐全。(3)

 5.字符串的替换:一般使用replace()方法就可以,translate,maketrans合用比较复杂,这里不介绍。

复制代码
 1 s = 'Hello world Python'
 2 
 3 print(s.replace('o', '0'))  # 将字符串中所有的o 替换成0 默认全部替换
 4 
 5 print(s.replace('o', '0', 2))  # 第三个参数,是指定替换的个数
 6 
 7 # 输出结果:     Hell0 w0rld Pyth0n
 8 #               Hell0 w0rld Python
 9 
10  ----------------------------------------------------------------------------------
11 # translate, maketrans
12 # 按照对应关系来替换内容
13 
14 
15 s = 'Hello world,'
16 a = 'world,'
17 b = 'python'
18 ret = str.maketrans(a, b)
19 s1 = s.translate(ret)  # 必须一一对应
20 print(s1)
21 
22 # 输出结果: Hehhy python
复制代码

6.字符串的修饰:center(),ljust(),rjust(),zfill()方法。

复制代码
 1 # center:将字符串按指定长度居中,若果不能左短右长,可以指定填充内容,默认以空格填充。
 2  
 3 s = 'hello'
 4  
 5 print(s.center(10,'*'))   # 填充长度是10 ,用 * 填充
 6  
 7 输出结果:**hello***
 8 -------------------------------------------------------------------------------------
 9 # ljust:让字符串在指定的长度左齐,可以指定填充内容,默认以空格填充
10       
11 # rjust:让字符串在指定的长度右齐,可以指定填充内容,默认以空格填充。
12  
13 # ljust 和 rjust
14 s = 'hello'
15 print(s.ljust(15, '~'))
16 print(s.rjust(15, '*'))
17  
18 输出结果:hello~~~~~~~~~~
19          **********hello
20 -----------------------------------------------------------------------------------------
21 # zfill:  将字符串填充到指定长度,不足的地方从左开始填充0
22  
23 s = 'hello'
24  
25 print(s.zfill(10))
26  
27 输出结果:00000hello
28 -----------------------------------------------------------------------------------------
29 # strip: 去除空白符
30 
31 poem = [
32     "\t\n你好大玛",
33     "哥哥,弟弟你好\r",
34     "小妹你好",
35     "小姐你好\t\n",
36 ]
37 print(poem)
38 输出为:['\t\n你好大玛', '哥哥,弟弟你好\r', '小妹你好', '小姐你好\t\n']
39 
40 
41 for poem_str in poem:
42      print(poem_str.strip().center(10))  # 空格填充剧中,并去除空白符。
43 输出为:
44    你好大玛   
45  哥哥,弟弟你好  
46    小妹你好   
47    小姐你好 
复制代码

 7.字符串的变形转换:upper(),lower(),swapcase(),title(),capitalize(),expandtabs()方法。

复制代码
 1 # upper   将字符串中所有字母转换为大写
 2 
 3 # lower   将字符串中所有字母转化为小写
 4 
 5 # swapcase 将字符串中字母大小写互换
 6 
 7 # title    将字符串中单词首字母转化为大写 
 8 
 9 # capitalize 只有字符串首字母大写
10 
11 # expandtabs 将字符串中('\t')符号转化为tab(4个空格)
12 # -------------------------------------------------------------------------------------
13 s = 'Hello python'
14 
15 print(s.upper())
16 
17 print(s.lower())
18 
19 print(s.swapcase())
20 
21 print(s.title())
22 
23 print(s.capitalize())
24 
25 s1 = 'hello\tpython'
26 
27 print(s1.expandtabs())
28 
29 
30 
31 -----------------------------------------------------------------------------------------

复制代码

 

  输出结果:HELLO PYTHON
                    hello python

                    hELLO PYTHON
                    Hello Python
                    Hello python
                    hello   python
 

8.字符串的格式化:format()方法。

复制代码
# format()用法:

# 相对基本格式化输出采用'%'的方法,format()功能更强大,
# 通过传入参数进行格式化,并使用大括号{}代替 %

# 1.使用位置参数:位置必须一一对应:

name = 'zs'

age = 19

print('大家好,我叫{},今年{}岁'.format(name, age))  # 位置必须一一对应

print('大家好,我叫{0},我叫{0},我叫{0},今年{1}岁'.format(name, age)) #索引位置对应参数

输出结果:大家好,我叫zs,今年19岁
        大家好,我叫zs,我叫zs,我叫zs,今年19岁
-----------------------------------------------------------------------------------------

# 2.使用关键字参数:

print('我叫{name},今年{age}岁了'.format(name='zs', age=19))

输出结果:我叫zs,今年19岁了
复制代码

 9.字符串的判断:

       大部分判断都是返回bool型

复制代码
 1 # 判断字符串中是否只包含数字(详细)
 2 numStr = "12414"
 3 numStr1 = "2312.22"
 4 numStr2 = ""
 5 numStr3 = "\u00b2"
 6 numStr4 = ""         # 输入法V+数字产生的数字符合
 7 
 8 # isdecimal只可以判断整数型字符串,不能判断小数等其他类型
 9 print("isdecimal分析结果")
10 print(numStr.isdecimal())  # True
11 print(numStr1.isdecimal())  # False
12 print(numStr2.isdecimal())  # False
13 print(numStr3.isdecimal())  # False
14 print(numStr4.isdecimal())  # False
15 
16 # isdigit可以判断整数型字符串,"\u00b2",⑴,但不能判断小数,汉字数字(大写)
17 print("isdigit分析结果")
18 print(numStr.isdigit())  # True
19 print(numStr1.isdigit())  # False
20 print(numStr2.isdigit())  # False
21 print(numStr3.isdigit())  # True
22 print(numStr4.isdigit())  # True
23 
24 # isnumeric可以判断整型数字,汉字的数字(大写数字),⑴,"\u00b2",但不能判断小数
25 print("isnumeric分析结果")
26 print(numStr.isnumeric())  # True
27 print(numStr1.isnumeric())  # False
28 print(numStr2.isnumeric())  # True
29 print(numStr3.isnumeric())  # True
30 print(numStr4.isnumeric())  # True
31 -----------------------------------------------------------------------------------------
32 
33 '''其他判断类型'''
34 
35 s = 'Hello123'
36 print(s.isalnum())  # True       判断字符串是否完全由数字和字母组成
37 
38 s1 = 'abcde'
39 print(s1.isalpha())  # True        判断字符串是否完全由字母组成
40 
41 s2 = 'abc123'
42 print(s2.isdigit())  # False          判断zfc是否完全由数字组成,上面已经详细介绍
43 
44 s3 = '  abcDE'
45 print(s3.isupper())  # False        判断字符串内字符是否全是大写
46 
47 s4 = 'abcd'
48 print(s4.islower())  # True        判断字符串内字符是否全是小写
49 
50 s5 = 'Hello Python'
51 print(s5.istitle())  # True       判断字符串中的单词首字母是否大写
52 
53 s6 = '     '
54 print(s6.isspace())  # True        判断字符串是否完全由空格组成
55 
56 s7 = 'hello python'
57 print(s7.startswith('h', 2, 4))  # False      判断字符串在指定范围内是否已指定字符开头
58 print(s7.endswith('lo', 0, 5))  # True       判断字符串在指定范围内是否已指定字符结尾
59 
60 s8 = "hello world"
61 print(s8.startswith("hello"))  # True       判断是否某个字符串开头
62 print(s8.endswith("ld"))  # True      判断是否某个字符串结尾
复制代码

 方法有如下:

              isdecimal(),isdigit(),isnumeric(),isalnum(),isalpha(),isupper(),islower(),istitle(),isspace(),startswith(),endswith()。

=====================================================================================================================================================
学习还有复查资料整合出来的,留着以后工作有用,或者学习经常查看,这里学习安之老师布置这个作业!虽然整合了我一天时间!-_-!
相关教程
        
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号