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

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    A problem with printing every single character in a string


    hello everyone, I am quite new to Python and got stuck with the following code:

    >>>a="Jack"
    >>>a
    >>>'Jack'
    >>>for achar in a:
    ... print achar

    the code above is supposed to print out the following:
    J
    a
    c
    k

    but it does work that way. Greatly appreciate for any help or guideline that can be provided.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    If it's not working correctly, what is it doing? Giving a syntax error? An exception? Printing something different?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by Nyktos
    If it's not working correctly, what is it doing? Giving a syntax error? An exception? Printing something different?
    it comes out with the following:
    SyntaxError: invalid syntax

    I am not quite sure of the correct syntax, because I just practiced it according to a reference book that I am learning Python from.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Which IDE are you using? In other words, what program are you doing this in? There's nothing wrong with the code.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    IDE used


    Originally Posted by StevenJM
    Which IDE are you using? In other words, what program are you doing this in? There's nothing wrong with the code.
    I am using IDLE Python 3.3. lol, I tried to find out the mistake with the code. And I think I got it. The code should goes:

    >>>a='Jack'
    >>>for achar in a:
    print (achar)
    J
    a
    c
    k
    >>>

    The code is lacking of '()'.
    But, thanks for the response though.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    That'd be the problem. The code you originally posted isn't compatible with Python 3.x.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Yeah based on this and the other thread you made, it sounds like the book you're reading is based on Python 2 while you're using Python 3. There are a lot of little changes between the two like this. This page has a summary of most of them; you may want to keep in bookmarked so you can check it whenever you're getting an error trying code from the book.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by Nyktos
    Yeah based on this and the other thread you made, it sounds like the book you're reading is based on Python 2 while you're using Python 3. There are a lot of little changes between the two like this. This page has a summary of most of them; you may want to keep in bookmarked so you can check it whenever you're getting an error trying code from the book.
    yeap. bookmarked. thank you very much.
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    I guess it's resolved, but you need a space (or some sort of indentation) before the statement with print() following the '... ' continuation prompt.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Originally Posted by b49P23TIvg
    I guess it's resolved, but you need a space (or some sort of indentation) before the statement with print() following the '... ' continuation prompt.
    Itís there all right (as you can see for yourself if you try to reply to the post) but it is not shown because there are no CODE tags.

    Comments on this post

    • b49P23TIvg agrees : Ah! I see. Thanks.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)

IMN logo majestic logo threadwatch logo seochat tools logo