What use for reversed()?
Mark Lawrence
breamoreboy at yahoo.co.uk
Sun May 31 20:30:40 EDT 2015
On 01/06/2015 00:23, Tim Delaney wrote:
> On 1 June 2015 at 05:40, fl <rxjwg98 at gmail.com
> <mailto:rxjwg98 at gmail.com>> wrote:
>
> Hi,
>
> I have a string b='1234'. I run: br=reversed(b)
>
> I hope that I can print out '4321' by:
>
> for br in b
>
> but it complains:
> SyntaxError: invalid syntax
>
>
> Any time you get a SyntaxError, it means that you have coded something
> which does not match the specified syntax of the language version.
>
> Assuming you copied and pasted the above, I can see an error:
>
> for br in b
>
> The for statement must have a colon at the end of line e.g. a complete
> for statement and block is:
>
> for br in b:
> print br
>
> This will output the characters one per line (on Python 3.x), since that
> is what the reversed() iterator will return. You will need to do
> something else to get it back to a single string.
Will it indeed? Perhaps fixing the syntax error will get something to
print :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Python-list
mailing list