0

i'm new in the python world and i have small issue. im trying to run this action for example:

print ('Hello World')

print('1','2')

But i'm getting this output:

Hello World

('1','2')

Why the second row is not showing properly?

I'm using a mac laptop with python version 2.7

Thanks for the help.

1
  • 1
    What do you mean with properly? Please give us the expected output. Also you should share your codes in Stack Overflow Community with quotes like this print ('Hello World') for avoiding formatted text problems. Commented Oct 15, 2015 at 9:57

3 Answers 3

1

Because () is used for grouping, it does not have an effect in the execution level but it makes your code look prettier:

 if ( 1 > 2 > 3):

is totally samw with

if 1 > 2 > 3:

Most important part is, (1) is not a tuple with a single element. Those parenthesis are evaluated as grouper, and have no effect in execution so it is totally same with 1. On the other hand, (1,) is a tuple and , after the first element implies that.

So

print ("Hello World")
print "Hello World"

are totally the same thing. But ('1','2') is a tuple so print statement prints it.

Sign up to request clarification or add additional context in comments.

Comments

1

You're trying to use the Python 3 print() function, not the Python 2 print command. However, you're trying to use this function in Python 2. Use from __future__ import print_function to get this behavior in Python 2.

See also __future__ and PEP 3105.

Comments

0

print ('Hello World') prints the string Hello World

print ('1','2') prints the tuple ('1','2').

3 Comments

Im not sure i get it, i try this : print('test','koko') and get this: ('test','koko') Instead test koko
Don't use ( and ) with print when you're working with Python 2.x. print '1', '2' is enough.
@Matthias - could you edit your answer to explain the difference between print '1', '2' and print('1', '2') in Python 2.X? It would improve the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.