Python 3


#1

Hi

How do you do this in Python 3?

phrase = "A bird in the hand..."

for char in phrase:
    if char is 'A' or char is 'a':
        print 'X',
    else:
        print char,

Output:

X   b i r d   i n   t h e   h X n d . . .
None

How do I get the same output in Python 3, since I need to use parenthesis in print() which causes the output to change to

X
 
b
i
r
d
 
i
n
 
t
h
e
 
h
X
n
d
.
.
.

#2

you could use end:


#4

but printing a new line is also done in python2, but for the rest, yes


#6

python3 is indeed not backward compatible with python2

well, there is still plenty of python2 code out there, so there is a change you encounter it. Converting from python2 to python3 can be expensive if you have a big code base

ideally, you would learn both. If you work with python long enough, you will not the major difference and most minors once, if you ever come across something obscure i am sure there is a stackoverflow question about it


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.