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.

asked Jul 8, 2016 at 15:05
\$\endgroup\$
1
  • \$\begingroup\$ The second part of your question ("raw" io) belongs on Stack Overflow instead. \$\endgroup\$ Commented 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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.