如何编辑字符串以在 python 中的特定位置添加变量?

How to edit a string to add a variable at a particular place in python?

我有一个变量:

一=2

我有这个字符串:

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

我想编辑这个字符串,这样我就可以在这个字符串的特定位置添加变量 a,在“mzz11035_black_xl”之后,使整个字符串看起来像:

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

我有什么选择可以实现这个目标。我知道有一些硬编码的方法可以让我计算特定位置前后的字符并进行切片,但我正在寻找一些更通用的方法,这样即使字符串稍微改变它也能工作。例如

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

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

谢谢。

您需要使用 f 字符串或 format() 函数:

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"

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)

如果“mzz11035_black_xl”的位置发生变化,您可以这样做:

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]

但在这种情况下,您需要确保您的字符串中只有一个“mzz11035_black_xl”。


如果字符串在变化,但 link 结构没有变化,您可以尝试用“/”拆分(现在不优雅,但可以优化)

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)

输出:

"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"

你可以使用replace()功能。

语法:

string.replace(old, new, count)

参数:

  • old – 您要替换的旧子字符串。
  • new – 将替换旧子字符串的新子字符串。
  • count – 您要用新子字符串替换旧子字符串的次数。 (可选)

Return 值:

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

代码示例:

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)