•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,534 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,968 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1123 | Replies: 11
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Hi, this is my first forum message so I'll make a brief introduction. I'm from Canada and I am Computer Science student. I enjoy working with computers, specially web programming. It has come to my attention (and also a school requirement) to learn Python. So here I am with my first question:
Suppose I have the following list:
And I want to make each element from list lowercase. This is what I have so far:
That will lowercase each of my elements. Up till that part everything is fine. However, let's say I want to return the string again, but this time. With all its elements properly lowercased. And this is where I'm stuck. I can't think of a way to do this.
Any help / ideas / suggestions will be greatly appreciated.
Suppose I have the following list:
python Syntax (Toggle Plain Text)
list = ["CANADA", "HELLO", "I TOLD YOU SO"]
And I want to make each element from list lowercase. This is what I have so far:
python Syntax (Toggle Plain Text)
for char in list: char.lower()
That will lowercase each of my elements. Up till that part everything is fine. However, let's say I want to return the string again, but this time. With all its elements properly lowercased. And this is where I'm stuck. I can't think of a way to do this.
Any help / ideas / suggestions will be greatly appreciated.
•
•
Join Date: Jun 2007
Posts: 27
Reputation:
Rep Power: 2
Solved Threads: 1
>>> LIST = ["CANADA", "HELLO", "I TOLD YOU SO"]
>>> conv_list=[]
>>> for el in LIST:
conv_list +=[el.lower()]
>>> conv_list
['canada', 'hello', 'i told you so']•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Interesting, could you explain me a bit this part?
What exactly would the += do there?
python Syntax (Toggle Plain Text)
conv_list +=[el.lower()]
What exactly would the += do there?
•
•
Join Date: Sep 2007
Posts: 19
Reputation:
Rep Power: 2
Solved Threads: 2
•
•
•
•
>>> LIST = ["CANADA", "HELLO", "I TOLD YOU SO"] >>> conv_list=[] >>> for el in LIST: conv_list +=[el.lower()] >>> conv_list ['canada', 'hello', 'i told you so']
You might also try:
list_in = ["CANADA", "HELLO", "I TOLD YOU SO"] list_out = [] for element in list_in: list_out.append(element.lower()]
Python has a nifty feature called list comprehensions that would also make a good solution, but if you haven't come across them yet don't worry:
list_in = ["CANADA", "HELLO", "I TOLD YOU SO"] list_out = [element.lower() for element in list_in]
- Paddy.
•
•
•
•
Interesting, could you explain me a bit this part?
python Syntax (Toggle Plain Text)
conv_list +=[el.lower()]
What exactly would the += do there?
python Syntax (Toggle Plain Text)
my_list = ["CANADA", "HELLO", "I TOLD YOU SO"] conv_list=[] for el in my_list: conv_list = conv_list + [el.lower()] print conv_list """ my output --> ['canada', 'hello', 'i told you so'] """
python Syntax (Toggle Plain Text)
my_list = ["CANADA", "HELLO", "I TOLD YOU SO"] conv_list=[] for el in my_list: conv_list.append(el.lower()) print conv_list """ my output --> ['canada', 'hello', 'i told you so'] """
May 'the Google' be with you!
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
Perfect, yes I was aware of the append method. That's the way I did it after Striker put his solution up. I just didn't see it all the way through. Thanks a lot folks.
Now let me complicate this a bit more. What if I only want to lowercase all the letters but the first letter from each string included in the list? For example, we have:
I want it to show up like:
["Canada", "hello", "Can"]
I was thinking in using an if-else statement but I don't know whether to check the first letter for each string on a list is capital or not.
Now let me complicate this a bit more. What if I only want to lowercase all the letters but the first letter from each string included in the list? For example, we have:
python Syntax (Toggle Plain Text)
list = ["CAnADA", "hELLO", "CAN"]
I want it to show up like:
["Canada", "hello", "Can"]
I was thinking in using an if-else statement but I don't know whether to check the first letter for each string on a list is capital or not.
•
•
Join Date: Sep 2007
Posts: 19
Reputation:
Rep Power: 2
Solved Threads: 2
This should help:
- Paddy.
>>> s = 'CAnADA' >>> s.capitalize() 'Canada' >>>
- Paddy.
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
depending on what you want ...
strings have number of powerful methods; you might poke around with
dir(str)
and
help(str.M), where M is some method name.
Jeff
Python Syntax (Toggle Plain Text)
>>> s = "now is the time for ALL good men..." >>> s.capitalize() 'Now is the time for all good men...' >>> s.title() 'Now Is The Time For All Good Men...' >>>
strings have number of powerful methods; you might poke around with
dir(str)
and
help(str.M), where M is some method name.
Jeff
Last edited by jrcagle : Oct 8th, 2007 at 11:12 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Help with inline lists for horizontal navigation (HTML and CSS)
- Linked Lists C++ Sorting Integers (C++)
- help...one-dimensional array averaging program (C++)
- function for merging two lists (C++)
- Looking up and displaying values in linked lists (C++)
- Compare 2 Lists of Words (MySQL)
- bdk1.1 and j2sdk (Java)
Other Threads in the Python Forum
- Previous Thread: initalze class by a condition
- Next Thread: What's your favorite error?



Linear Mode