ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python: 数据类型的操作

2022-06-25 23:35:08  阅读:145  来源: 互联网

标签:info Python 数据类型 字符串 str new print 操作 message


字符串的常用方法

 

 

 

 

 

 

代码示例:

  1 """
  2 字符串的操作
  3 """
  4 # 首字母大写的方法
  5 str = 'dawei xiaoming'
  6 info = 'hello 小明'
  7 num_str = '1314'
  8 
  9 newstr = str.capitalize()
 10 new_info = info.capitalize()
 11 new_num_str = num_str.capitalize()
 12 print(newstr)
 13 print(new_info)
 14 print(new_num_str)
 15 
 16 # 字符串全部小写的方法lower()/casefold() 这两个方法几乎一致,lower()只对英文有效,casefold()可对其他语种有效
 17 message_en = 'How do you do? XiaoMing'
 18 message_ch = '你好呀,XiaoMing'
 19 message_mix = '你好呀,XiaoMing,今天星期3!'
 20 
 21 message_en_lower = message_en.lower()
 22 message_en_casefold = message_en.casefold()
 23 
 24 message_ch_lower = message_ch.lower()
 25 message_ch_casefold = message_ch.casefold()
 26 
 27 message_mix_lower = message_mix.lower()
 28 message_mix_casefold = message_mix.casefold()
 29 
 30 print( message_en_lower, message_en_casefold )
 31 print( message_ch_lower, message_ch_casefold )
 32 print( message_mix_lower, message_mix_casefold )
 33 
 34 # 字符串全部大写的方法upper()
 35 info = 'hello word! hello xiaoming'
 36 
 37 big_info = info.upper()
 38 small_info = info.lower()
 39 
 40 print(big_info)
 41 print(small_info)
 42 
 43 # 字符串中大小写字母进行转换的方法swapcase()
 44 info_one = 'Python Code Is Good'
 45 info_two = 'PYTHON DJANGO FLASK'
 46 info_three = 'python web so easy'
 47 
 48 info_one_new = info_one.swapcase()
 49 info_two_new = info_two.swapcase()
 50 info_three_new = info_three.swapcase()
 51 
 52 print(info_one_new)
 53 print(info_two_new)
 54 print(info_three_new)
 55 
 56 # 字符串zfill()填充方法
 57 info = 'love'
 58 
 59 print(' t   ' + info)
 60 print('t    ' + info)
 61 print(info.zfill(10))
 62 print(info.zfill(9))
 63 print(info.zfill(8))
 64 print(info.zfill(6))
 65 print(info.zfill(4))
 66 
 67 
 68 # 字符串中成员的个数count()方法
 69 info = """
 70 Python now uses the same ABI whether it's built in release or debug mode.on Unix,when Python is built in debug mode,
 71 it is now possible to load cextensions built in release mode and c extensions built using the stableABI.
 72 
 73 """
 74 a = info.count('a')
 75 b = info.count('b')
 76 c = info.count('c')
 77 d = info.count('d')
 78 e = info.count('e')
 79 
 80 
 81 print(a,b,c,d,e)
 82 num_list = [a,b,c,d,e]
 83 print('在列表中最大的数值是:', max(num_list))
 84 
 85 num_dict = {
 86     'a':a,
 87     'b':b,
 88     'c':c,
 89     'd':d,
 90     'e':e
 91 }
 92 print( '每个成员对应的数值是:',num_dict )
 93 
 94 
 95 # 判断字符串开始位和结尾的方法startswith()/endswith()
 96 info = 'this is a string example!!'
 97 
 98 print('字符串开始位匹配:',info.startswith('this'))
 99 print('字符串完全匹配:',bool(info == 'this is a string example!!'))
100 
101 print('字符串结尾匹配:',info.endswith('!'))
102 print('字符串结尾匹配:',info.endswith('this'))
103 
104 # 获取字符串中某个值的位置方法find()和index()方法
105 info = 'python is a good code'
106 
107 print(info.find('a'))
108 print(info.find('ok'))  # find()方法找不到该元素位置时,返回-1
109 
110 print(info.index('a'))
111 print(info.index('ok'))  # index()方法找不到该元素位置时,程序报错
112 
113 
114 # 去除字符串左右两边的元素或空格的方法strip();lstrip():仅去掉字符串开头的指定元素或空格;
115 
116 info_01 = '  my name is xiaoming  '
117 new_info_01 = info_01.strip()
118 print('.' + new_info_01 + '.')  # 去除字符串左右两边的空格
119 
120 new_str = 'abcde'
121 print( new_str.strip('a') )  # 去除字符串左右两边的a元素
122 print( new_str.lstrip('a') )  # 去除字符串左边的a元素
123 print( new_str.rstrip('e') )  # 去除字符串右边的e元素
124 
125 
126 # 替换字符串中元素的方法replace()方法
127 info = """
128 要从小白到一个有经验的开发者,无论是通过视频还是文字教程学习,你会发现很少有初级课程就非常贴近实际工作的,
129 作为一个刚入坑的小白通常并不知道需要学习什么,往往是自认为入门的时候都学习了,到了公司里才发现很多都不会。
130 我希望做这样一个课程,虽是入门课程,但涉及相关领域的多处知识,让小白在学习后进入公司岗位不会因为没听过而蒙圈;
131 同时希望这个课也可以帮助非Python工程师怏速转型或者快速转职能)
132 """
133 a = '小白'
134 b = '一个'
135 c = '蒙圈'
136 d = '课程'
137 e = '*'
138 f = '@'
139 g = '$'
140 h = '&'
141 
142 new_str_01 = info.replace(a,e).replace(b,f).replace(c,g).replace(d,h)
143 print(new_str_01)
144 
145 new_str = info.replace(a,e)
146 new_str = new_str.replace(b,f)
147 new_str = new_str.replace(c,g)
148 new_str = new_str.replace(d,h)
149 print(new_str)
150 
151 # 字符串中返回bool类型的函数集合
152 title = 'Back Of China'
153 upper_str = 'PYTHON IS A GOOD CODE'
154 lower_str = 'i love python'
155 not_empty = ' '
156 
157 # istitle() 判断字符串是否是一个标题类型(标题类型是字符串中每个首字母都是大写为标题类型)
158 print(title.istitle())
159 # isupper() 判断字符串中的字母是否都是大写
160 print(upper_str.isupper())
161 # islower() 判断字符串中的字母是否都是小写
162 print(lower_str.islower())
163 # isspace() 判断字符串是否是一个由空格组成的字符串
164 print(not_empty.isspace())
165 
166 # 字符串格式化操作 %,format,f-string
167 str_01 = 'my name is %s,my age is %.2f'
168 print(str_01 % ('xiaoming',33))
169 
170 str_02 = 'my name is {0},my age is {1}'
171 print(str_02.format('xiaobai',20))
172 
173 name = 'xiaoxin'
174 age = 28
175 str_03 = f'my name is {name},my age is {age}'
176 print(str_03)

 

标签:info,Python,数据类型,字符串,str,new,print,操作,message
来源: https://www.cnblogs.com/YouJeffrey/p/16412705.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有