Python Forum
String handling - 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: String handling (/thread-21108.html)

Pages: 1 2 3


String handling - YoungGrassHopper - Sep-14-2019

Hey guys.

Here is what I have to accomplish:

● Ask the user to input a string and then ask the user which characters they would like to make disappear.
For example: The quick brown fox jumps over the lazy dog. 
o After stripping a,e,i,o,u 
o Th qck brwn fx jmps vr th lzy dg.  

Here is what I came up with but clearly my code is flawed somewhere, can someone please point out where I am going wrong with this.

string = input("Enter a sentence here: ")
characters = input("Enter the characters which you would like to remove from the sentence above. Example: 'W','k','p','o' \n")
stripped_str = string.strip(characters)
print(stripped_str)
 



RE: String handling - metulburr - Sep-14-2019

its better to hard code and remove input() when posting on the forums such as
string = 'The quick brown fox jumps over the lazy dog. '
characters = 'aeiou'
stripped_str = string.strip(characters)
print(stripped_str)
one way is to use list comprehension to bring a for loop into one line
print(''.join([c for c in string if c not in characters]))
Output:
Th qck brwn fx jmps vr th lzy dg.



RE: String handling - YoungGrassHopper - Sep-14-2019

(Sep-14-2019, 10:41 AM)metulburr Wrote: its better to hard code and remove input() when posting on the forums such as
string = 'The quick brown fox jumps over the lazy dog. '
characters = 'aeiou'
stripped_str = string.strip(characters)
print(stripped_str)
one way is to use list comprehension to bring a for loop into one line
print(''.join([c for c in string if c not in characters]))
Output:
Th qck brwn fx jmps vr th lzy dg.

Thanks metulburr, noted and I appreciate the help big time.


RE: String handling - YoungGrassHopper - Sep-15-2019

(Sep-14-2019, 10:41 AM)metulburr Wrote: its better to hard code and remove input() when posting on the forums such as
string = 'The quick brown fox jumps over the lazy dog. '
characters = 'aeiou'
stripped_str = string.strip(characters)
print(stripped_str)
one way is to use list comprehension to bring a for loop into one line
print(''.join([c for c in string if c not in characters]))
Output:
Th qck brwn fx jmps vr th lzy dg.


if its not too much trouble would you be so kind as to give a quick explanation as to how your code executes? How it all fits together and how it works, I see there is a for loop, and a if statement which I am more or less familiar with by now, the .join seems very obvious. I have not yet heard about the "not in" which follows the if statement. It also sounds very obvious as to what it does but I would really love to hear how exactly it works so I can possibly replicate it in the future. It runs perfectly but I would love to understand it perfectly too if a short explanation is not too much of a problem for you. This one liner for loops is quite foreign to me


RE: String handling - perfringo - Sep-15-2019

'give me char for every char in string which is not in (forbidden) characters':

[char for char in string if char not in characters]
Now there is list of characters (including non-printable whitespaces) and using ''.join new string is constructed from this.


RE: String handling - ndc85430 - Sep-15-2019

Regarding in, see the documentation, though it is pretty self-explanatory.


RE: String handling - YoungGrassHopper - Sep-15-2019

Thanks @perfringo that settles my lack of understanding,
And thanks @ndc85430 for path to the info. I think this is solved now appreciate all the input.


RE: String handling - snippsat - Sep-15-2019

In a ordinary loop it would be like this.
>>> string = 'The quick brown fox jumps over the lazy dog. '
>>> characters = 'aeiou'
>>> 
>>> lst = []
>>> for c in string:
...     if c not in characters:
...         lst.append(c)
...         
>>> print(''.join(lst))
Th qck brwn fx jmps vr th lzy dg.
So a list comprehension construct make code over in line.

Can also remove the list as it's make a generator object first.
>>> g = (c for c in string if c not in characters)
>>> g
<generator object <genexpr> at 0x03C8C8B0>

>>> next(g)
'T'
>>> next(g)
'h'
See that there is a small difference no [],and it still work as ''.join() run the generator expressions.
>>> print(''.join(c for c in string if c not in characters))
Th qck brwn fx jmps vr th lzy dg. 



RE: String handling - YoungGrassHopper - Sep-15-2019

(Sep-15-2019, 09:55 AM)snippsat Wrote: In a ordinary loop it would be like this.
>>> string = 'The quick brown fox jumps over the lazy dog. '
>>> characters = 'aeiou'
>>> 
>>> lst = []
>>> for c in string:
...     if c not in characters:
...         lst.append(c)
...         
>>> print(''.join(lst))
Th qck brwn fx jmps vr th lzy dg.
So a list comprehension construct make code over in line.

Can also remove the list as it's make a generator object first.
>>> g = (c for c in string if c not in characters)
>>> g
<generator object <genexpr> at 0x03C8C8B0>

>>> next(g)
'T'
>>> next(g)
'h'
See that there is a small difference no [],and it still work as ''.join() run the generator expressions.
>>> print(''.join(c for c in string if c not in characters))
Th qck brwn fx jmps vr th lzy dg. 


Thanks snippsat Smile
The ordinary loops is much softer on my understanding as I have only worked with a couple of them till date and haven't been taught or tasked to use a for loop as a one liner. the one liners though look more simple as there seems no need for indentation and all that.

Since this thread is named string handling and the use of for loops are being discussed.
I have another task bugging me which I need advice on.

So I have a txt file with a list of names surnames and date of births.
it looks like this:

Orville Wright 21 July 1988
Rogelio Holloway 13 September 1988
Marjorie Figueroa 9 October 1988
Debra Garner 7 February 1988

And so on.

I need to write python code which reads in the data from the list and restructures and prints it in this format:

Name:
Debra Garner    

Birthdate:
7 February 1988

My attempt :

f = open
doc = f.read()
print(doc.replace(' ','\n'))
f.close()                                                                                                             
Now I am getting:

Debra
Surname
21
Aug
1999


Is it possible to refer to the second white space in a line or how would one efficiently go about solving this?


RE: String handling - jefsummers - Sep-15-2019

This may nudge you in the right direction. Just doing it for one entry, but you know how to loop now to repeat this...
str = "Orville Wright 21 July 1988"
strlist = str.split()
print (strlist)
name = strlist[0]+" "+strlist[1]
print(f"Name:\n{name}")
dob = strlist[2]+" "+strlist[3]+" "+strlist[4]
print("You take it from here")