A detailed description of the use of lower and upper functions in Python

Source: Internet
Author: User
Today, a little bit of a problem, you need to write a program to the list of irregular English names in the "first letter uppercase the rest of the lowercase" way to convert

The first idea was to write a function to process each string "str" and how to batch it with a map

I have to say that the general direction of the idea is correct, but in the details of the treatment of some problems

At first I wrote the name-handling function:

[Python] View plain copydef Change (name)      result = Name[0].upper () to      A in Name[1:len (name)]:          result = result + A.lower ()

Then the run will error

[Python] View plain copye:\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 one, in <module> Name_li = map (change, name_list) file "practice.py", Li NE 6, in change for a in name (1,len (name)-1): TypeError: ' str ' object was not callable E:\pythonfile>python PR  actice.py Traceback (most recent): 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 supp  ORT item Assignment E:\pythonfile>python practice.py Traceback (most recent call last): File ' practice.py ', line  <module> Name_li = map (change, name_list) File ' practice.py ', line 6, ' change name[0] = tmp TypeError: ' Str ' object Does not support item assignment E:\pythonfile>python python 2.7.9 (default, Dec, 12:24:55) [MSC v.1500 32  Bit (Intel)] on Win + 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 objec T at 0x02148740> >>> print l[0].upper () z >>> z Traceback (most recent call last): File "<s Tdin> ", line 1, in <module> nameerror:name ' z ' are not defined >>> ^z E:\pythonfile>python pra ctice.py Traceback (most recent): File ' practice.py ', line 9, ' in <module> name_li = Map ' (change, name_list) File "practice.py", line 6, with change name[0] = tmp TypeError: ' str ' object does not support item Assi Gnment E:\pythonfile>python practice.py Traceback (most recent call last): File "practice.py", line +, in <m odule> Name_li = map (change, name_list)    File "practice.py", line 5, with change TMP = Chr (name[0]) Typeerror:an integer is required 

After several attempts to find the approximate meaning of the string processing can not do so, then checked the use of upper () and lower (), found that these two functions are directly in STR, so there is no need for such trouble.

After modification:

[Python] View plain copy# practice.py # change the first  char in every name from lower type to upper type    def Chan GE (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

Operation Result:

[Python] View plain copye:\pythonfile>python practice.py  [' Kzd ', ' ysy ', ' KCW ', ' Scr ', ' Ky ']

The error is that I'm trying to replace the elements in the string with an assignment, which is not allowed.

"Recommended"

1. Python Free video tutorial

2. Introduction to the basics of Python Upper

3. How to convert case in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.