Python Forum

Full Version: Is this good style?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just want to know whether this is good style:

print("The length of your string is {} characters.".format(
      len(input("Enter a string: "))))
Also, is this a statement or an expression? I always get confused about those two.
It is a callback.
However, I don't like it. It's hard to read.

text = input("Enter a string: ")
print("The length of your string is {} characters.".format(len(text)))
Or:

text_len = len(input("Enter a string: "))
print("The length of your string is {} characters.".format(text_len))
Or:

text = input("Enter a string: ")
text_len = len(text)
print("The length of your string is {} characters.".format(text_len))
It's much easier to see what is going on.
i prefer the 3rd option as it explicitly states each line what it is doing.
In python 3 it is a function call, therefore an expression, and as such it is also a statement (an expression statement).

I tend not to include calls to input() in complex expressions, but apart from this, it is good python style.