Python 中的lower()、upper()函数和str\chr

今天遇到一点小问题,需要编写一个程序将List 中的无规则英文名按【首字母大写其余部分小写】的方式转换

一开始的思路是编写一个函数将每个名字字符串【str】进行处理,如何用map进行批处理

不得不说,大方向的思路是正确的,但在细节的处理上出了些问题

最开始我是这样写名字处理函数的:

def change (name)
    result = name[0].upper()
    for a in name[1:len(name)]:
        result = result + a.lower()
然后运行会报错

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的,所以根本不需要这么麻烦。

修改后:

# 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
运行结果:

E:\PythonFile>python practice.py
['Kzd', 'Ysy', 'Kcw', 'Scr', 'Ky']


错误在于我企图对字符串中的元素进行赋值替换,这是不允许的。




  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值