Re: Python string replace the values

2017-09-02 Thread Peter Otten
Steve D'Aprano wrote: > On Sat, 2 Sep 2017 03:13 am, Ganesh Pal wrote: > >> Example : >> >> "a" and "1" => a0001 >> >> "a" and "aa" => c00aa > > Why does the leading 'a' change to a 'c'? Is that a mistake? I'll assume > its a typo. > > You want string slicing. > > base = 'a'

Re: Python string replace the values

2017-09-02 Thread Ganesh Pal
MRAB , Thanks for your solution it looks neat and best ! On Fri, Sep 1, 2017 at 11:14 PM, MRAB wrote: > On 2017-09-01 18:13, Ganesh Pal wrote: > >> In the fixed length string i.e "a",last 4 bits i.e "" should be >> replaced by the user provided value ( the value is between 0001 + f f f

Re: Python string replace the values

2017-09-01 Thread Steve D'Aprano
On Sat, 2 Sep 2017 03:13 am, Ganesh Pal wrote: > Example : > > "a" and "1" => a0001 > > "a" and "aa" => c00aa Why does the leading 'a' change to a 'c'? Is that a mistake? I'll assume its a typo. You want string slicing. base = 'a' extra = 'ab' print( base[:-len(extra)] + extra

Re: Python string replace the values

2017-09-01 Thread Ganesh Pal
>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,

Re: Python string replace the values

2017-09-01 Thread Ganesh Pal
> (I assume that in your example "a" and "aa" => c00aa, that really should have been a00aa) Yes , thanks for noticing. On Fri, Sep 1, 2017 at 11:18 PM, Irv Kalb wrote: > > > On Sep 1, 2017, at 10:13 AM, Ganesh Pal wrote: > > > > In the fixed length string i.e "a",last 4 bits i.e "00

Re: Python string replace the values

2017-09-01 Thread Irv Kalb
> On Sep 1, 2017, at 10:13 AM, Ganesh Pal wrote: > > In the fixed length string i.e "a",last 4 bits i.e "" should be > replaced by the user provided value ( the value is between 0001 + f f f f ) > . I intend to form final_string using a fixed_string and an user provided > user_string >

Re: Python string replace the values

2017-09-01 Thread Rhodri James
On 01/09/17 18:13, Ganesh Pal wrote: "a" + "1"===> expected was a0001 'a1' 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.

Re: Python string replace the values

2017-09-01 Thread MRAB
On 2017-09-01 18:13, Ganesh Pal wrote: In the fixed length string i.e "a",last 4 bits i.e "" should be replaced by the user provided value ( the value is between 0001 + f f f f ) . I intend to form final_string using a fixed_string and an user provided user_string Example: fixed_st