如何按位置将变量插入字符串

问题描述

如何按位置将变量插入字符串, 例如,在{ ... "require": { ... "ext-gd": "*",... } ... }

中插入a

string

string = "Hello World"

a = '@'

输出>>> Hel @ lo World

解决方法

str_1="Hello World"
st=list(str_1)
st.insert(pos,a)
str_1="".join(st)

您可以首先将列表转换为字符,然后将其存储在列表中,在列表中,您可以添加任意数量的元素,然后最终使用.join()

获得修改后的字符串。 ,

您可以使用切片:

a = '@'
position = 3
string = "Hello World"
new = string[:position] + a + string[position:]
,

您可以使用此:

for i in range(self.height):
    for j in range(self.width):

        self.world[i][j] = noise.pnoise3(i/self.noise['scale'],j/self.noise['scale'],SEED,octaves = self.noise['octaves'],persistence = self.noise['persistence'],lacunarity = self.noise['lacunarity'],repeatx= self.width,repeaty= self.height,base= 0)