Splitting a line into parts
When entering a string, you can immediately divide it into parts by any separator.
We've done this before when we entered multiple numeric variables on the same line. We used the
split()
method to split a string into parts. By default, it separates strings into substrings by space.
Using this method, you can split the string into parts, for example, by spaces. And write each part of the string into a separate variable.
Example
s1, s2 = input().split()
In the example, when entering two words separated by a space, the first word is stored in the variable
s1
, the second - in the variable
s2
.