Skip to content
Advertisement

What’s the difference between assigning a string value within and without parentheses for a variable in Python 3?

message = "Hello World!"

vs

message = ("Hello World!")

As far as I have tested, both get interpreted with the same output. I’ve just started to learn python and wondering if those parentheses have any impact.

Advertisement

Answer

Both the implementations are same.

message= "Hello World!"
print(type(message))

Output: <class ‘str’>

message = ("Hello World!")
print(type(message))

Output: <class ‘str’>

Adding parenthesis to a string don’t automatically make them tuples. You need to add a comma after the string to indicate to python that it should be a tuple.

message = ("Hello World!",)
print(type(message))

Output:<class ‘tuple’>

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement