# 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.
-
\$\begingroup\$ The second part of your question ("raw" io) belongs on Stack Overflow instead. \$\endgroup\$Perry– Perry2016年07月08日 15:18:06 +00:00Commented Jul 8, 2016 at 15:18
1 Answer 1
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
-
2\$\begingroup\$
print(s_string)
will print a blank line ifs_string
is empty. \$\endgroup\$TheBlackCat– TheBlackCat2016年07月08日 15:52:16 +00:00Commented Jul 8, 2016 at 15:52 -
\$\begingroup\$ Oh, indeed! Thanks for pointing that out, will edit. \$\endgroup\$Majiick– Majiick2016年07月08日 15:53:25 +00:00Commented Jul 8, 2016 at 15:53