[Tutor] How to manipulate a user's input twice in one statement
Alan Gauld
alan.gauld at yahoo.co.uk
Tue May 16 04:38:55 EDT 2023
On 16/05/2023 06:39, Goran Ikac wrote:
> If a value is assigned to the string some_string, this works:
>>>> some_string = 'avion'
>>>> some_string.removesuffix(input()[1:])
> ton
> 'avi'
It would help us understand what you are trying to do if you added
prompts to your input statements:
some_string = 'avion'
some_string.removesuffix(
input("provide suffix with extra character at the front")[1:])
> But the next statement doesn't work as I've expected (it waits for another
> user's input):
Because you have two input statements. It will execute both of them.
>>>> input().removesuffix(input()[1:])
> avion
> ton
> 'avi'
Why do you want it all on one line? Normally you'd capture the two
things then process them:
some_string = input("String to process: ")
suffix = input("enter a suffix with an extra letter: ")
print(some_string.remove_suffix(suffix[1:])
Which is easier to read and debug and helps your user.
> Now, is there a way to manipulate the input from a user so to remove a
> slice from it, other than to assign that slice to a variable name and then
> to manipulate that variable?
You are not assigning a slice you are *applying* a slice to the string.
It's not clear why you are using a slice but that is not relevant.
The real question is why you want to do it all on one line?
There is no advantage and several disadvantages.
Remember the >>> prompt is not where your real programs will run,
that's just for experimenting and testing. The real code will run
using print statements etc. And you can place the input/output
exactly where you want it.
> I'd like to be able to put the user's input as an argument for a method
> that manipulates that same user's input. The method .removesuffix() is only
> an example.
Sorry, I'm still not clear what you really want to do.
Can you give us an example of a user session (without code for now)
Just show us the inputs and expected outputs?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list