Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Print string in a single line
#1
'''
Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order.
Sample 
My name is Camilla 
output: Camilla is name My
-PSEUDOCODE-
1) Ask user a string 
2) Get string and break it 
3) Change the order of string 
4) Union the string 

STRING ARE LISTS !!!!
 string = "example"
  for c in string: 
    print "one letter: " + c 

'''

string_input = input("Insert a long phrase: ")

#print(string_input) test if input is print

split_string = string_input.split()
#print(split_string) test if string become list 

for word_break in reversed(split_string):
  

  

 print( "".join(word_break))
Output:
Insert a long phrase: My name is Camilla Camilla is name My
Hi,
I would that these words are display in a single line but I tried to change my loop statement and never is changed.
Can you help me?
Quote
#2
print( "".join(word_break),end="")
Quote
#3
You don't need a loop, join can do that for you. Just pass the reversed list of words (all of them) to join.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#4
(Nov-07-2019, 03:05 PM)baquerik Wrote:
print( "".join(word_break),end="")

Thanks it works!!!

(Nov-07-2019, 03:06 PM)ichabod801 Wrote: You don't need a loop, join can do that for you. Just pass the reversed list of words (all of them) to join.

I see that you can use also reverse() but I saw this solution in Stackoverflow to reverse a list
Quote
#5
(Nov-07-2019, 03:10 PM)RavCOder Wrote:
(Nov-07-2019, 03:05 PM)baquerik Wrote:
print( "".join(word_break),end="")

Hi!

You could also do:

string_input = input("Insert a long phrase: ") 
split_string = string_input.split()   
print(*reversed(split_string), sep=' ')
Output:
Output:
Insert a long phrase: My name is Camilla Camilla is name My >>>
All the best,
ichabod801 likes this post
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Quote
#6
(Nov-07-2019, 03:24 PM)newbieAuggie2019 Wrote:
(Nov-07-2019, 03:10 PM)RavCOder Wrote:

Hi!

You could also do:

string_input = input("Insert a long phrase: ") 
split_string = string_input.split()   
print(*reversed(split_string), sep=' ')
Output:
Output:
Insert a long phrase: My name is Camilla Camilla is name My >>>
All the best,

Thanks I will check like alternative solution
Quote
#7
I personally have no respect for courses and tutors who write in assignment "STRING ARE LISTS !!!!".

Strings are iterables. Strings are sequences. But this doesn't make them lists.

Whoever wrote it should be faced with:

>>> s = 'abc'
>>> type(s)
<class 'str'>
>>> isinstance(s, list)
False
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#8
Hi perfringo,

I didn't know that string aren't list, but I maybe I think that they refered a string is immutable like a list (then I don't know if this is correct or not).
Forgive me if I said something wrong.
RavCoder
Quote
#9
(Nov-08-2019, 08:52 AM)RavCOder Wrote: I maybe I think that they refered a string is immutable like a list (then I don't know if this is correct or not).

Strings are immutable and list are mutable
sequences.

I recommend to read documentation: Sequence Types — list, tuple, range

Quote:There are three basic sequence types: lists, tuples, and range objects. Additional sequence types tailored for processing of binary data and text strings are described in dedicated sections.

There are described Common Sequence Operations, Mutable and Immutable Sequences, and Text Sequence types.

Strings and lists are iterable.

Difference between sequence and iterable can be described as: sequence has order i.e. one can access items by index (str, list, tuple, range) but iterable is capability to return one item at the time and therefore supports in addition to sequences un-ordered datastructures like sets as well.

But in no circumstances one should consider string as list.
RavCOder likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to print string multiple times separated by delimiter Mekala 1 92 May-23-2020, 09:21 AM
Last Post: Yoriz
  print only last matched line tester_V 24 756 Apr-30-2020, 05:16 AM
Last Post: deanhystad
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 148 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Print only nth line AgileAVS 2 175 Feb-05-2020, 03:47 PM
Last Post: snippsat
  How to print the 2nd column of a searched string in a csv ptey07 2 226 Jan-11-2020, 11:25 AM
Last Post: ptey07
  Print string after decode martinzeifang 1 252 Jan-02-2020, 10:16 AM
Last Post: buran
  Python convert multi line into single line formatted string karthidec 2 507 Dec-23-2019, 12:46 PM
Last Post: karthidec
  Select single letters from string based on separate string Jpen10 3 563 Dec-15-2019, 01:21 PM
Last Post: Jpen10
  How to print message after row value in the same line SriRajesh 2 249 Dec-07-2019, 03:17 PM
Last Post: perfringo
  line number of first and second occurance of string in a file mdalireza 1 225 Nov-18-2019, 09:55 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)