Home > Backend Development > Python Tutorial > Detailed explanation of the use of lower and upper functions in python

Detailed explanation of the use of lower and upper functions in python

Y2J
Release: 2017-05-18 14:55:15
Original
13123 people have browsed it

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()
Copy after login

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: &#39;str&#39; 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: &#39;str&#39; 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: &#39;str&#39; 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 = &#39;zxcv&#39;  
>>> 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 &#39;z&#39; 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: &#39;str&#39; 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
Copy after login

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 = [&#39;kzd&#39;,&#39;ysy&#39;,&#39;kcw&#39;,&#39;scr&#39;,&#39;ky&#39;]  
name_li = map (change, name_list)  
print name_l
Copy after login

Run result:

[python] view plain copy
E:\PythonFile>python practice.py  
[&#39;Kzd&#39;, &#39;Ysy&#39;, &#39;Kcw&#39;, &#39;Scr&#39;, &#39;Ky&#39;]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template