-2

I want to place a value into a string, however I cannot access the string as a f-string because it has multiple curly braces, but also I cannot use the following %s to access a value in the string as it has multiple % operators.

Here's an example string:

string = '{{id,name,tagline,frequency,duration,price,formattedPrice,pricePerUnit,formattedPricePerUnit}oneTimePurchase{index}}priceRange(withSubscriptionPriceRange:true),@include(if:$withPriceRange){fromPriceFormatted}discount{mode,value}currency,weight,seoJson}}}&v=%7B%22slug%22%3A%22p8-by-olly-fathers%22%2C%22externalId%22%3A%22%22%2C%22withPriceRange%22%3Afalse%7D'

I wanted to include the following string:

firstRange

inside the following part of the string:

externalId%22%3A%22%2firstRange%2C%22withPriceRange%22%3Afalse%7D

I have tried using:

string % "firstRange"

Aftering inserting %s in the required position but I get the following error:

ValueError: unsupported format character 'B' (0x42)
tesla john
  • 310
  • 1
  • 2
  • 9
  • 1
    You can escape a brace in an f-string by prefixing it with another matching brace: `f'{{foo}}' == '{foo}'` – Iain Shelvington Feb 24 '22 at 16:59
  • Why are you using `%` for string formatting? This is super old. At the very list use the `str.format` method and for Python >= 3.6 use f-strings... – Tomerikoo Feb 24 '22 at 17:10

2 Answers2

1

How about:

P = '%22%3A%22%22'
F = 'firstRange'
string = '{{id,name,tagline,frequency,duration,price,formattedPrice,pricePerUnit,formattedPricePerUnit}oneTimePurchase{index}}priceRange(withSubscriptionPriceRange:true),@include(if:$withPriceRange){fromPriceFormatted}discount{mode,value}currency,weight,seoJson}}}&v=%7B%22slug%22%3A%22p8-by-olly-fathers%22%2C%22externalId%22%3A%22%22%2C%22withPriceRange%22%3Afalse%7D' 

if (idx := string.find(P)) >= 0:
    string = string[:idx+len(P)] + F + string[idx+len(P):]
    print(string)
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

By using double curly brackets you can "escape" them:

'{hi} {}'.format('hey') #don't use this, instead use:
'{{hi}} {}'.format('hey')

So for your case:

string = '{{{{id,name,{}#ADDEDTHISFORTESTINGtagline,frequency,duration,price,formattedPrice,pricePerUnit,formattedPricePerUnit}}oneTimePurchase{{index}}}}priceRange(withSubscriptionPriceRange:true),@include(if:$withPriceRange){{fromPriceFormatted}}discount{{mode,value}}currency,weight,seoJson}}}}}}&v=%7B%22slug%22%3A%22p8-by-olly-fathers%22%2C%22externalId%22%3A%22%22%2C%22withPriceRange%22%3Afalse%7D'

string = string.format('test')
print(string)

Output:

{{id,name,test#ADDEDTHISFORTESTINGtagline,frequency,duration,price,formattedPrice,pricePerUnit,formattedPricePerUnit}oneTimePurchase{index}}priceRange(withSubscriptionPriceRange:true),@include(if:$withPriceRange){fromPriceFormatted}discount{mode,value}currency,weight,seoJson}}}&v=%7B%22slug%22%3A%22p8-by-olly-fathers%22%2C%22externalId%22%3A%22%22%2C%22withPriceRange%22%3Afalse%7D

Notice that 'test' was added as the 3rd word in place of the two curly brackets I added:

'{}#ADDEDTHISFORTESTING' #this
#changed to:
'test#ADDEDTHISFORTESTING' #this
#via
string = string.format('test')
Eli Harold
  • 2,280
  • 1
  • 3
  • 22