On 2017-09-01 18:13, Ganesh Pal 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

How long is user_string? len(user_string)
Truncate fixed_string by that many characters: fixed_string[ : -len(user_string)]

Then append the user_string: fixed_string[ : -len(user_string)] + user_string

Example:

>>> fixed_string = "a0000"
>>> user_string ='1'
>>> len(user_string)
1
>>> fixed_string[ : -len(user_string)]
'a000'
>>> fixed_string[ : -len(user_string)] + user_string
'a0001'

Note: this won't work properly if len(user_string) == 0, so if that could happen, just skip it. (Appending an empty string has no effect anyway!)
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to