Skip to main content
added 85 characters in body
Source Link
wjandrea
  • 720
  • 9
  • 22

Python 3

s = input()  # Take one line of input from stdin.
x = len(s) // 2  # Get middle of string. "//" is floor division
print(s[:x], s[x:], sep="\n")  # Print "s" up to "x", then "s" past "x", joined on newlines.
print(s[:x], s[x:], sep="\n")

For example,

$ echo abcdef | python3 -c 's = input(); x = len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
def

If the string length is not an even number, the second line will be longer. E.g.

$ echo abcdefg | python3 -c 's = input(); x= len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
defg

Python 3

s = input()
x = len(s) // 2
# Print "s" up to "x", then "s" past "x", joined on newlines.
print(s[:x], s[x:], sep="\n")

For example,

$ echo abcdef | python3 -c 's = input(); x = len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
def

If the string length is not an even number, the second line will be longer. E.g.

$ echo abcdefg | python3 -c 's = input(); x= len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
defg

Python 3

s = input()  # Take one line of input from stdin.
x = len(s) // 2  # Get middle of string. "//" is floor division
print(s[:x], s[x:], sep="\n")  # Print "s" up to "x", then "s" past "x", joined on newlines.

For example,

$ echo abcdef | python3 -c 's = input(); x = len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
def

If the string length is not an even number, the second line will be longer. E.g.

$ echo abcdefg | python3 -c 's = input(); x= len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
defg
Source Link
wjandrea
  • 720
  • 9
  • 22

Python 3

s = input()
x = len(s) // 2
# Print "s" up to "x", then "s" past "x", joined on newlines.
print(s[:x], s[x:], sep="\n")

For example,

$ echo abcdef | python3 -c 's = input(); x = len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
def

If the string length is not an even number, the second line will be longer. E.g.

$ echo abcdefg | python3 -c 's = input(); x= len(s) // 2; print(s[:x], s[x:], sep="\n")'
abc
defg