Python Forum
How do we print this output?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do we print this output?
#1
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!
Reply
#2
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.
Reply
#3
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!
Reply
#4
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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Hi
I need to print this output:
[S1 is Today is a sunny day]
Reply
#6
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 '.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print not appearing in output dgizzly 1 1,220 Oct-14-2022, 09:55 PM
Last Post: rob101
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,932 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Need to print the output as follows Mac 2 3,326 Jul-19-2017, 06:52 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020