0

I have a variable:

a = 2

and I have this string:

"//media.boohooman.com/i/boohooman/mzz11035_black_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"

I want edit this string so I can add variable, a, at a specific place in this string, after "mzz11035_black_xl", to make the whole string look like:

"//media.boohooman.com/i/boohooman/mzz11035_black_xl_2/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"

What options do I have to achieve this goal. I know that there are some hard coded ways where I can count the characters before and after a specific place and do slicing, but I am looking for some more general method so that it would work even if the strings change a bit. eg.

"//media.boohooman.com/i/boohooman/mzz11035_blue_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"

or

"//media.boohooman.com/i/boohooman/mzz11035_blue_s/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"

Thanks.

2 Answers 2

2

You need to use f string or the format() function:

var = "_2"
f"//media.boohooman.com/i/boohooman/mzz11035_black_xl{var}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"

or

var = "_2"
"//media.boohooman.com/i/boohooman/mzz11035_black_xl{}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp".format(var)

if the position of the "mzz11035_black_xl" is changing you can do this:

var = "_2"
split_with =  "mzz11035_black_xl"
initial_string = "//media.boohooman.com/i/boohooman/mzz11035_black_xl{var}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
# split the string into two parts
split_string = initial_string.split(split_with)
#  Add the parts back with any sting in between
resulting_string = split_string[0] + split_with + var + split_string[1]

but in this case, you need to make sure that you have only one "mzz11035_black_xl" in your string.


If the string is changing but the link structure doesn't change you can try splitting with "/" (not elegant now but can be optimized)

var = "_2/"
split_with = "/"
initial_string = "//media.boohooman.com/i/boohooman/mzz11035_blue_s/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
# initializing K
K = 6
# printing original string
print("The original string is : " + str(initial_string))
 
# Using split() + join()
# Split string on Kth Occurrence of Character
temp = initial_string.split(split_with)
resulting_tuple = split_with.join(temp[:K]), split_with.join(temp[K:])
# Convert to list and insert any string 
resulting_list = list(resulting_tuple)
resulting_list.insert(1,var)
# convert to string
resulting_string = ""
resulting_string = resulting_string.join(list(resulting_list))

print("Is list after Kth split is: " + resulting_string)

Output:

"Is list after Kth split is : //media.boohooman.com/i/boohooman/mzz11035_blue_s_2/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, sorry for not being clear before. I have edited my post a bit to make it more clear.
The third method will work even if the position of the "mzz11035_black_xl" is changing or if the string is changing. Another alternative is to split with "/" characters. How else is the link supposed to change?
0

you can use replace() function.

Syntax:

string.replace(old, new, count)

Parameters:

  • old – old substring you want to replace.
  • new – new substring which would replace the old substring.
  • count – the number of times you want to replace the old substring with the new substring. (Optional)

Return Value:

It returns a copy of the string where all occurrences of a substring are replaced with another substring.

Code sample:

a = "2"
str_to_replace = "//media.boohooman.com/i/boohooman/mzz11035_black_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
old = "mzz11035_black_xl"
new = "mzz11035_black_xl_{}".format(a)
str_replaced = str_to_replace.replace(old, new)
print(str_replaced)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.