详解python中lower和upper函数的使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了详解python中lower和upper函数的使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3116字,纯文字阅读大概需要5分钟。
内容图文
![详解python中lower和upper函数的使用](/upload/InfoBanner/zyjiaocheng/431/481d93ba08cc4eb29fba1fe996f0589e.jpg)
一开始的思路是编写一个函数将每个名字字符串【str】进行处理,如何用map进行批处理
不得不说,大方向的思路是正确的,但在细节的处理上出了些问题
最开始我是这样写名字处理函数的:
[python] view plain copy def change (name) result = name[0].upper() for a in name[1:len(name)]: result = result + a.lower()
然后运行会报错
[python] view plain copy E:\PythonFile>python practice.py File "practice.py", line 6 for a in name(1:len(name)-1) : ^ SyntaxError: invalid syntax E:\PythonFile>python practice.py Traceback (most recent call last): File "practice.py", line 11, in <module> name_li = map (change, name_list) File "practice.py", line 6, in change for a in name(1,len(name)-1) : TypeError: 'str' object is not callable E:\PythonFile>python practice.py Traceback (most recent call last): File "practice.py", line 9, in <module> name_li = map (change, name_list) File "practice.py", line 5, in change name[0] = name[0].upper() TypeError: 'str' object does not support item assignment E:\PythonFile>python practice.py Traceback (most recent call last): File "practice.py", line 10, in <module> name_li = map (change, name_list) File "practice.py", line 6, in change name[0] = tmp TypeError: 'str' object does not support item assignment E:\PythonFile>python Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> l = 'zxcv' >>> print l[0] z >>> print l[0].upper <built-in method upper of str object at 0x02148740> >>> print l[0].upper() Z >>> z Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'z' is not defined >>> ^Z E:\PythonFile>python practice.py Traceback (most recent call last): File "practice.py", line 9, in <module> name_li = map (change, name_list) File "practice.py", line 6, in change name[0] = tmp TypeError: 'str' object does not support item assignment E:\PythonFile>python practice.py Traceback (most recent call last): File "practice.py", line 13, in <module> name_li = map (change, name_list) File "practice.py", line 5, in change tmp = chr(name[0]) TypeError: an integer is required
试过几次后发现大致意思是对字符串的处理不能这么做,于是查了upper()和lower()的用法,发现这两个函数就是直接作用于str的,所以根本不需要这么麻烦。
修改后:
[python] view plain copy # practice.py # Change the first char in every name from lower type to upper type def change (name): result = name[0:1].upper()+name[1:len(name)].lower() return result name_list = ['kzd','ysy','kcw','scr','ky'] name_li = map (change, name_list) print name_l
运行结果:
[python] view plain copy E:\PythonFile>python practice.py ['Kzd', 'Ysy', 'Kcw', 'Scr', 'Ky']
错误在于我企图对字符串中的元素进行赋值替换,这是不允许的。
【相关推荐】
1. Python免费视频教程
2. python基础入门之upper简介
3. python中转换大小写的方法
以上就是 详解python中lower和upper函数的使用的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的详解python中lower和upper函数的使用全部内容,希望文章能够帮你解决详解python中lower和upper函数的使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。