Python converts a string to a tuple

Example:

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

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

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

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

+3
source share
2 answers

Because of the comma at the end of the line.

+16
source

When you write

result["somekey"] = regular_string,

Python reads

result["somekey"] = (regular_string,)

(x,)is the syntax of a single-element tuple. Brackets are assumed. And you really insert a tuple instead of a string.

+9
source

Source: https://habr.com/ru/post/1712393/


All Articles