Discuss List comprehensions in the Python Programming forum on Dev Shed. List comprehensions Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Posts: 44
Time spent in forums: 4 h 10 m 54 sec
Reputation Power: 0
List comprehensions
Hello, what I am trying to do is change the user input to all caps just for practice with list comprehensions. But as soon as I enter some input the program quits. So I am like
here is my code
Code:
input_string = str(input(' your letters here: '))
[chars.upper() for chars in input_string]
Posts: 3,387
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 10 m 30 sec
Reputation Power: 383
It works. Note that in python3 input returns a string, so str(input()) is a bit wasteful. Now, if you're trying to run this as a module and you want to see the result you'd need to explicitly print result of the expression.
print([chars.upper() for chars in input_string])
Code:
$ python3
Python 3.2.3 (default, Sep 10 2012, 18:14:40)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input_string = str(input(' your letters here: '))
your letters here: a;kjas;lkf
>>> [chars.upper() for chars in input_string]
['A', ';', 'K', 'J', 'A', 'S', ';', 'L', 'K', 'F']
>>>
__________________
[code]Code tags[/code] are essential for python code!
Posts: 483
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
If you don't use the Python shell, you may need to add a wait:
Code:
# Python3 syntax
input_string = input(' your letters here: ')
print([chars.upper() for chars in input_string])
# wait for user response
input("Press Enter to go on ...")
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25