哪个更快-更高效-更合适?
加入
string = "Hello"
concatenate = " ".join([string, "World"])
print(concatenate)
或
格式
string = "Hello"
concatenate = f"{string} World"
print(concatenate)
最佳答案
我们可以使用 join() 函数将字符串与分隔符连接起来。当我们有一系列字符串时它很有用,例如字符串列表或元组。 比方说 -
s1 = 'Hello'
s2 = 'World'
print('Concatenated String using join() =', "".join([s1, s2]))
Output:
Concatenated String using join() = HelloWorld
print('Concatenated String using join() and whitespaces =', " ".join([s1, s2]))
Output:
Concatenated String using join() and spaces = Hello World
.format 函数 - 我们也可以使用 string format() 函数进行字符串连接和格式化
s1 = 'Hello'
s2 = 'World'
s3 = f'{s1} {s2}'
print('String Concatenation using f-string =', s3)
String Concatenation using f-string = Hello World
name = 'Pankaj'
age = 34
d = Data(10)
print(f'{name} age is {age} and d={d}')
Pankaj age is 34 and d=Data[10]
关于python - `join` 或 `format` 字符串哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62367044/
相关文章:
reactjs - WebSocket SocketIO 连接无法与 Heroku 上的 NestJ
python-3.x - pynput pip3 安装错误 : Could not find a v
ios - 如何从 iOS (Swift) 连接到 MongoDB
c# - 开拓者。如何在 Startup 类(方法 ConfigureServices)中获取当前
linux - docker 内部出现 fatal error : unable to access
linux - tar: 目录: 无法 rmdir: 目录不为空