Python string replace the values

Ganesh Pal ganesh1pal at gmail.com
Fri Sep 1 14:07:29 EDT 2017


>Note you should think VERY CAREFULLY about any user input like this. What
are you going to do the user gives you too many digits, too few digits,
non-digits

Thanks the solution i.e using zfill()  looks simple :) , we have a check
that will take care of user input than's for noticing that

On Fri, Sep 1, 2017 at 11:16 PM, Rhodri James <rhodri at kynesim.co.uk> wrote:

> On 01/09/17 18:13, Ganesh Pal wrote:
>
>> "a0000" + "1"    ===> expected was a0001
>>>>>
>>>> 'a00001'
>>
>
> Why would you expect that?  Concatenation means "joining together", so
>
> >>> "foo" + "bar"
> 'foobar'
>
> No other mangling of either of your original strings is going to happen;
> there is no magic going on here.
>
> What you want is most easily done using the zfill() string method to pad
> out the user's string with zeroes:
>
> >>> fixed_string = "a"
> >>> user_string = "1"
> >>> final_string = fixed_string + user_string.zfill(4)
> 'a0001'
>
> Note you should think VERY CAREFULLY about any user input like this. What
> are you going to do the user gives you too many digits, too few digits,
> non-digits?
>
> --
> Rhodri James *-* Kynesim Ltd
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list