Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String handling
#1
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)
 
Reply
#2
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.
Recommended Tutorials:
Reply
#3
(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.
Reply
#4
(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
Reply
#5
'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.
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.
Reply
#6
Regarding in, see the documentation, though it is pretty self-explanatory.
Reply
#7
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.
Reply
#8
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. 
Reply
#9
(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?
Reply
#10
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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,215 Nov-09-2022, 07:29 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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