Home >Backend Development >Python Tutorial >Detailed explanation of the use of lower and upper functions in python
I encountered a small problem today. I need to write a program to convert the irregular English names in the List into [capitalize the first letter and lowercase the rest]
The initial idea was to write a function to convert each name into uppercase. To process a name string [str], how to use map for batch processing
I have to say that the general idea is correct, but there are some problems in the detailed processing
At first, I wrote the name processing function like this:
[python] view plain copy def change (name) result = name[0].upper() for a in name[1:len(name)]: result = result + a.lower()
Then an error will be reported when running
[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
After trying it a few times, I found that the general meaning is that string processing cannot be done in this way, so I checked Regarding the usage of upper() and lower(), I found that these two functions directly act on str, so there is no need to be so troublesome at all.
After modification:
[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
Run result:
[python] view plain copy E:\PythonFile>python practice.py ['Kzd', 'Ysy', 'Kcw', 'Scr', 'Ky']
The error is that I tried to assign and replace the elements in the string, which is not allowed.
【Related Recommendations】
1. Free video tutorial on Python
2. Introduction to python basic introduction to upper
3. How to convert uppercase and lowercase in python
The above is the detailed content of Detailed explanation of the use of lower and upper functions in python. For more information, please follow other related articles on the PHP Chinese website!