Re: How to replace space in a string with \n

2019-01-31 Thread Terry Reedy
On 1/31/2019 1:36 PM, Grant Edwards wrote: On 2019-01-31, Grant Edwards wrote: On 2019-01-31, Terry Reedy wrote: On 1/31/2019 11:19 AM, Ian Clark wrote: text = "The best day of my life!" output = '' for i in text: if i == ' ': output +='\n' else: output += i print(output)

Re: How to replace space in a string with \n

2019-01-31 Thread Neil Cerutti
On 2019-01-31, ^Bart wrote: > Hello everybody! :) > > I got a text and I should replace every space with \n without to use > str.replace, I thought something like this: > > text = "The best day of my life!" > > space = (' ') > > if text.count(' ') in text: > space=\n > > rightText =

Re: How to replace space in a string with \n

2019-01-31 Thread Grant Edwards
On 2019-01-31, ^Bart wrote: > Hello everybody! :) > > I got a text and I should replace every space with \n without to use > str.replace, I thought something like this: > > text = "The best day of my life!" [...] > I should have an output like this: > The > best > day > of > my > life! Here's

Re: How to replace space in a string with \n

2019-01-31 Thread Chris Angelico
On Fri, Feb 1, 2019 at 5:34 AM Grant Edwards wrote: > > On 2019-01-31, Terry Reedy wrote: > > On 1/31/2019 11:19 AM, Ian Clark wrote: > >> text = "The best day of my life!" > >> output = '' > >> > >> for i in text: > >> if i == ' ': > >>output +='\n' > >> else: > >>output += i > >> >

Re: How to replace space in a string with \n

2019-01-31 Thread Grant Edwards
On 2019-01-31, Grant Edwards wrote: > On 2019-01-31, Terry Reedy wrote: >> On 1/31/2019 11:19 AM, Ian Clark wrote: >>> text = "The best day of my life!" >>> output = '' >>> >>> for i in text: >>> if i == ' ': >>>output +='\n' >>> else: >>>output += i >>> >>> print(output) > >> But

Re: How to replace space in a string with \n

2019-01-31 Thread Grant Edwards
On 2019-01-31, Terry Reedy wrote: > On 1/31/2019 11:19 AM, Ian Clark wrote: >> text = "The best day of my life!" >> output = '' >> >> for i in text: >> if i == ' ': >>output +='\n' >> else: >>output += i >> >> print(output) > But this is an awful, O(n*n) way to solve an inherently

Re: How to replace space in a string with \n

2019-01-31 Thread Terry Reedy
On 1/31/2019 11:19 AM, Ian Clark wrote: text = "The best day of my life!" output = '' for i in text: if i == ' ': output +='\n' else: output += i print(output) throwing my hat in the ring, not only is it .replace free it is entirely method free But this is an awful, O(n*n) way to

RE: How to replace space in a string with \n

2019-01-31 Thread Avi Gross
It is amazing to watch what happens when a fairly simple question is asked to see how people answer. In an effort not to ramble, I will summarize my thoughts. The student wanted to know how to solve a problem using only what they already should know and that specifically they should not use a

Re: How to replace space in a string with \n

2019-01-31 Thread Ian Clark
text = "The best day of my life!" output = '' for i in text: if i == ' ': output +='\n' else: output += i print(output) throwing my hat in the ring, not only is it .replace free it is entirely method free On Thu, Jan 31, 2019 at 3:41 AM ^Bart wrote: > [Solved by myself and I'm happy!!!

Re: How to replace space in a string with \n

2019-01-31 Thread ^Bart
[Solved by myself and I'm happy!!! :)] text = "The best day of my life!" space = text[3] print (text.replace(space, "\n")) [Like what I said in another message, in the afternoon I'll ask to the teacher if for this exercise we're able to use .replace] --

Re: How to replace space in a string with \n

2019-01-31 Thread Michael Poeltl
hi ^Bert, I've just thought that you don't like to use text.replace(' ', '\n'), and so I came up with another way to get the job done. So it was part of a "school-test" - uiuitststs ;-) follow the hint from Peter then, and inside *your* for-loop ask yourself, how to inspect the value of c in

Re: How to replace space in a string with \n

2019-01-31 Thread Chris Angelico
On Thu, Jan 31, 2019 at 9:56 PM ^Bart wrote: > > >You coulde use »sub« from the module »re«, then. > >(The Python Library Reference, Release 3.8.0a0 - > >6.2 re - Regular expression operations) > > We're using 3.7.2 :\ > Don't worry about that difference - 3.8 is only minorly

Re: How to replace space in a string with \n

2019-01-31 Thread Antoon Pardon
On 31/01/19 11:47, ^Bart wrote: >>    . A correct answer to the exercise would be: >> >> |You cannot replace a space with \n in a string, >> |because strings are immutable in Python. > > Yes, I thought in a wrong way! :) > Well maybe you can turn the string into a list of characters. Then replace

Re: How to replace space in a string with \n

2019-01-31 Thread ^Bart
No it is not the proper way of a school test to copy what others provided. You're right but I need just to understand what tools I should use, it could be nice if the teacher says something like "use just these three tools to solve this problem" or "you don't need to use these other tools to

Re: How to replace space in a string with \n

2019-01-31 Thread ^Bart
You coulde use »sub« from the module »re«, then. (The Python Library Reference, Release 3.8.0a0 - 6.2 re - Regular expression operations) We're using 3.7.2 :\ Otherwise, Write a loop that takes the first character from the source and appends it to a new string until

Re: How to replace space in a string with \n

2019-01-31 Thread ^Bart
. A correct answer to the exercise would be: |You cannot replace a space with \n in a string, |because strings are immutable in Python. Yes, I thought in a wrong way! :) -- https://mail.python.org/mailman/listinfo/python-list

Re: How to replace space in a string with \n

2019-01-31 Thread ^Bart
Il 31/01/19 10:34, Michael Poeltl ha scritto: hi, Maybe this is a proper way to do what you'd liked to achieve text = "The best day of my life!" newtext = '\n'.join( text.split() ) print(newtext) The best day of my life! yours Michael Thanks Michael, I'll ask to my teacher in the

Re: How to replace space in a string with \n

2019-01-31 Thread ^Bart
Have you even tried to run this? No, it doesn't run, it's just a personal idea! :) I don't think this does what you think it does. text.count(' ') will return 5, an integer. So you are testing if 5 is in text. But since 5 is an integer that will raise a TypeError. Yes, I understood this

Re: How to replace space in a string with \n

2019-01-31 Thread Antoon Pardon
On 31/01/19 10:37, Michael Poeltl wrote: > hi, > > ^Bart ended in a Mail-Delivery... > so I send it ONLY to the python-list > > ^Bert, a proper way to do what you'd liked to achieve is the following: No it is not the proper way of a school test to copy what others provided. -- Antoon. --

Re: How to replace space in a string with \n

2019-01-31 Thread Antoon Pardon
On 31/01/19 10:18, ^Bart wrote: > Hello everybody! :) > > I got a text and I should replace every space with \n without to use > str.replace, I thought something like this: Have you even tried to run this? > > text = "The best day of my life!" > > space = (' ') > > if text.count(' ') in text: >  

Re: How to replace space in a string with \n

2019-01-31 Thread Peter Otten
^Bart wrote: >> Why? > > It's a school test, now we should use just what we studied, if than, > else, sequences, etc.! > > ^Bart Hint: you can iterate over the characters of a string >>> for c in "hello": ... print(c) ... h e l l o --

Re: How to replace space in a string with \n

2019-01-31 Thread Michael Poeltl
hi, ^Bart ended in a Mail-Delivery... so I send it ONLY to the python-list ^Bert, a proper way to do what you'd liked to achieve is the following: >>> text = "The best day of my life!" >>> newtext = '\n'.join( text.split() ) >>> print(newtext) The best day of my life! >>> regards Michael *

Re: How to replace space in a string with \n

2019-01-31 Thread Michael Poeltl
hi, Maybe this is a proper way to do what you'd liked to achieve >>> text = "The best day of my life!" >>> newtext = '\n'.join( text.split() ) >>> print(newtext) The best day of my life! >>> yours Michael * ^Bart [2019-01-31 10:22]: > Hello everybody! :) > > I got a text and I should replace

Re: How to replace space in a string with \n

2019-01-31 Thread Michael Poeltl
maybe this is an alternative way to get your wished result. >>> text = "The best day of my life!" >>> newtext = '\n'.join( text.split() ) >>> print(newtext) The best day of my life! >>> yours Michael * ^Bart [2019-01-31 10:22]: > Hello everybody! :) > > I got a text and I should replace every

Re: How to replace space in a string with \n

2019-01-31 Thread ^Bart
Why? It's a school test, now we should use just what we studied, if than, else, sequences, etc.! ^Bart -- https://mail.python.org/mailman/listinfo/python-list

Re: How to replace space in a string with \n

2019-01-31 Thread Alister via Python-list
On Thu, 31 Jan 2019 10:18:20 +0100, ^Bart wrote: > Hello everybody! :) > > I got a text and I should replace every space with \n without to use > str.replace, Why? > I thought something like this: > > text = "The best day of my life!" > > space = (' ') > > if text.count(' ') in text: >

How to replace space in a string with \n

2019-01-31 Thread ^Bart
Hello everybody! :) I got a text and I should replace every space with \n without to use str.replace, I thought something like this: text = "The best day of my life!" space = (' ') if text.count(' ') in text: space=\n rightText = text-space print(rightText) I should have an