1
\$\begingroup\$
# printstring.py
# 7/8/2016

import sys

# ask for user input
s_string = input('Enter a string: ')

# if no string, then exit()
if s_string == '':
    sys.exit()

# display the output
print(s_string)

Also wondering how to get "raw" input and output the text properly formatted with line breaks, etc. Also looking for general feedback and tips for improvement.

\$\endgroup\$
1
  • \$\begingroup\$ The second part of your question ("raw" io) belongs on Stack Overflow instead. \$\endgroup\$
    – pppery
    Jul 8, 2016 at 15:18

1 Answer 1

2
\$\begingroup\$

More elegant way to check if a string is empty Read here

if not s_string:
    sys.exit()

You don't need a sys.exit(), just let the program exit on its own.

if s_string:
    print(s_string)

# the program will exit here anyway.  

sys.exit() has some side-effects like killing the python interpreter, which might have some undesirable effects. Read here

You don't even need to check if the string is empty

print(s_string) # If string is empty, will print nothing except for a newline.

Name s_string just s or string.

It doesn't detract from the readability of the code, and makes it more pretty and easier to type(and read)

Most people tend to prefer double quotes " for string literals Read Here

\$\endgroup\$
2
  • 2
    \$\begingroup\$ print(s_string) will print a blank line if s_string is empty. \$\endgroup\$ Jul 8, 2016 at 15:52
  • \$\begingroup\$ Oh, indeed! Thanks for pointing that out, will edit. \$\endgroup\$
    – Majiick
    Jul 8, 2016 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.