Python string replace the values
Rhodri James
rhodri at kynesim.co.uk
Fri Sep 1 13:46:26 EDT 2017
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
More information about the Python-list
mailing list