Python string replace the values
Irv Kalb
Irv at furrypants.com
Fri Sep 1 13:48:20 EDT 2017
> On Sep 1, 2017, at 10:13 AM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>
> In the fixed length string i.e "a0000",last 4 bits i.e "0000" 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_string = "a0000"
> user_string ='1'
>
> final string = fixed_string and user_string
>
> Example :
>
> "a0000" and "1" => a0001
>
> "a0000" and "aa" => c00aa
>
>
> PS : "and" this is not logical and it's just for example
>
> If I concatenation using + or += or it's append the value at the end of
> the string
>
>>>> "a0000" + "1" ===> expected was a0001
> 'a00001'
>
>
>
> I am on python 2.7 and Linux , any ideas that you recommend to handle
> this
>
>
> Regards,
> Ganesh
>
Here's a little function that should it it for you.
(I assume that in your example "a0000" and "aa" => c00aa, that really should have been a00aa)
This should work for any size fixed_string and user_string (as long as the user_string is not longer than the fixed_string)
def mergeStrings(fixed_string, user_string):
nCharsInFixedString = len(fixed_string)
nCharsInUserString = len(user_string)
nCharsToGetFromFixedString = nCharsInFixedString - nCharsInUserString
finalString = fixed_string[:nCharsToGetFromFixedString] + user_string
return finalString
Irv
More information about the Python-list
mailing list