Hi
I am trying to print this word output : s1 is today is a sunny
if i input this sentence "Today is a sunny day, let go to picnic"
I am unable to print the word "S1 is". I tried to use print ("s1 is" + str((s1[0:4]))
but meet error.
s1 = input("Enter 3 random strings, separated by commas:")
s1 = s1.split(',')
print (s1[0:4])
Appreciate for all advice. Thank you!
What is the error you get? Be more specific about what output you get and what output you want to have. See
this and
this.
Without knowing more, the only problem I see with
print ("s1 is" + str((s1[0:4]))
is that there is an extra closing parenthesis missing. The double parentheses around s1[0:4] are not needed though, single will do.
Hi
Apologies, The error is unable to use the str( s1[0:4]) in the print even though I have correct parentheses. Output I need is “ s1 is today is sunny day”. I have tried to use another method by assign a variable to contain the split of s1[0:2] and concatenation them in print with “s1 is” but unable to work too.
Appreciate for suggestions. Thanks!
The strings are three but your slicing is for a list with at least five elements -
[0:4]
.
"Today is a sunny day, let go to picnic" will become a two element list since it has only one comma.
>>> s1 = "Today is a sunny day, let go to picnic"
>>> s1 = s1.split(',')
>>> s1
['Today is a sunny day', ' let go to picnic']
>>> s1[0:4]
['Today is a sunny day', ' let go to picnic']
>>> print(s1[0:4])
['Today is a sunny day', ' let go to picnic']
>>>
What is the output you want to print?
Hi
I need to print this output:
[S1 is Today is a sunny day]
Well, split the sentence ( "Today is a sunny day, let go to picnic" ) and get the first element and you will have 'Today is a sunny day' which you can print alongside to 's1 is '.