#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    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]
    thanks for your help
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    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 and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    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 ...")

    Comments on this post

    • b49P23TIvg disagrees : learn to configure your DOS window so that it doesn't exit rather than writing incompatible programs.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3
    this forum is great thanks but how do I get the string returned to just be the characters for example

    if I enter 'hi' it would return
    ['H','I']

    but I want it to return

    HI

    any ideas?
    Last edited by Nick Wyden; October 20th, 2012 at 09:43 PM. Reason: I wanted to add one other question
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    study your string methods.

    ''.join(iterable_of_strings)
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3
    can you post some links about string methods?
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Post links about string methods?
    Bookmark www.python.org

    strings in the official python tutorial

    Official documents, text sequences

    The string module

    How to represent a string

    There's a lot of other useful information at docs.python.org .

    Also, help is available on just about everything in python. For instance, in the interpreter:

    >>> help('')


    Originally Posted by docs.python.org
    Library Reference
    keep this under your pillow
    Last edited by b49P23TIvg; October 21st, 2012 at 09:10 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3
    great! thanks

    Comments on this post

    • Dietrich agrees : learn and thou shall gain

IMN logo majestic logo threadwatch logo seochat tools logo