tefozi
tefozi

Reputation: 5480

Python converts string into tuple

Example:

regular_string = "%s %s" % ("foo", "bar")

result = {}
result["somekey"] = regular_string,

print result["somekey"]
# ('foo bar',)

Why result["somekey"] tuple now not string?

Upvotes: 3

Views: 2705

Answers (2)

Stefan Kanev
Stefan Kanev

Reputation: 3040

When you write

result["somekey"] = regular_string,

Python reads

result["somekey"] = (regular_string,)

(x,) is the syntax for a tuple with a single element. Parentheses are assumed. And you really end up putting a tuple, instead of a string there.

Upvotes: 9

Jiri
Jiri

Reputation: 16625

Because of comma at the end of the line.

Upvotes: 16

Related Questions