Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is this good style?
#1
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.
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
i prefer the 3rd option as it explicitly states each line what it is doing.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Style and Naming Variables rsherry8 3 256 Jun-07-2021, 09:30 PM
Last Post: deanhystad
  Tkinter font style ATARI_LIVE 2 307 Oct-24-2020, 08:22 PM
Last Post: ATARI_LIVE
  C style strings deanhystad 2 719 May-16-2020, 02:57 PM
Last Post: Larz60+
  Type hinting style and PEP8 hlovatt 2 793 May-07-2020, 08:12 PM
Last Post: hlovatt
  How to generate a log in a list style? wagnergt12 5 822 Apr-22-2020, 12:47 PM
Last Post: buran
  Type hints and style MaxPowers 1 701 Feb-19-2020, 06:56 PM
Last Post: micseydel
  Merging Dictionaries - Optimum Style? adt 5 895 Oct-09-2019, 05:26 PM
Last Post: adt
  Coding style questions -- variables Luke_Drillbrain 12 4,887 Apr-30-2017, 01:21 AM
Last Post: Luke_Drillbrain
  pdf sms alternating conversation style sdcaliber 4 2,836 Apr-20-2017, 05:20 AM
Last Post: sdcaliber

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020