Print string in a single line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Print string in a single line (/thread-22309.html) |
Print string in a single line - RavCOder - Nov-07-2019 ''' 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)) 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? RE: Print string in a single line - baquerik - Nov-07-2019 print( "".join(word_break),end="") RE: Print string in a single line - ichabod801 - Nov-07-2019 You don't need a loop, join can do that for you. Just pass the reversed list of words (all of them) to join. RE: Print string in a single line - RavCOder - Nov-07-2019 (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 RE: Print string in a single line - newbieAuggie2019 - Nov-07-2019 (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: All the best,
RE: Print string in a single line - RavCOder - Nov-07-2019 (Nov-07-2019, 03:24 PM)newbieAuggie2019 Wrote:(Nov-07-2019, 03:10 PM)RavCOder Wrote: Thanks I will check like alternative solution RE: Print string in a single line - perfringo - Nov-08-2019 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 RE: Print string in a single line - RavCOder - Nov-08-2019 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 RE: Print string in a single line - perfringo - Nov-08-2019 (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. |