避免在字符串中指定相同值的最python方法是什么?

时间:2022-06-02 22:19:45
message = "hello %s , how are you %s, welcome %s"%("john","john","john")

What is the most pythonic way to avoid specifying "john" 3 times and instead to specify one phrase.

避免三次指定“john”而指定一个短语的最python化的方法是什么?

4 个解决方案

#1


11  

"hello %(name)s , how are you %(name)s, welcome %(name)s" % {"name": "john"}
'hello john, how are you john, welcome john'

This is another way to do this without using format.

这是另一种不用格式的方法。

#2


24  

I wouldn't use % formatting, .format has many advantages. Also % formatting was originally planned to be removed with .format replacing it, although apparently this hasn't actually happened.

我不会使用% formatting, .format有很多优点。同样,% formatting原本计划用.format替换它来删除,但是显然这并没有发生。

A new system for built-in string formatting operations replaces the % string formatting operator. (However, the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.) Read PEP 3101 for the full scoop.

一个用于内置字符串格式化操作的新系统将替换% string格式化操作符。(但仍支持%运算符;它将在Python 3.1中被弃用,并在以后的某个时候从语言中删除。阅读PEP 3101获取完整的独家新闻。

>>> "hello {name}, how are you {name}, welcome {name}".format(name='john')
'hello john, how are you john, welcome john'

I prefer the first way since it is explicit, but here is a reason why .format is superior over % formatting

我喜欢第一种方式,因为它是显式的,但是这里有一个原因,为什么.format优于% formatting

>>> "hello {0}, how are you {0}, welcome {0}".format('john')
'hello john, how are you john, welcome john'

#3


3  

This works also:

这个作品也:

"hello %s , how are you %s, welcome %s"%tuple(["john"]*3)

or even shorter, without the explicit type cast:

甚至更短,没有明确的类型cast:

"hello %s , how are you %s, welcome %s"%(("john",)*3)

#4


1  

99% likely you should use .format()

99%可能你应该使用。format()

It's unlikely but if you had a series of greetings you could try this:

这不太可能,但如果你有一系列的问候,你可以试试这个:

>>> greetings = ["hello", "how are you", "welcome"]
>>> ", ".join(" ".join((greet, "John")) for greet in greetings)
'hello John, how are you John, welcome John'

#1


11  

"hello %(name)s , how are you %(name)s, welcome %(name)s" % {"name": "john"}
'hello john, how are you john, welcome john'

This is another way to do this without using format.

这是另一种不用格式的方法。

#2


24  

I wouldn't use % formatting, .format has many advantages. Also % formatting was originally planned to be removed with .format replacing it, although apparently this hasn't actually happened.

我不会使用% formatting, .format有很多优点。同样,% formatting原本计划用.format替换它来删除,但是显然这并没有发生。

A new system for built-in string formatting operations replaces the % string formatting operator. (However, the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.) Read PEP 3101 for the full scoop.

一个用于内置字符串格式化操作的新系统将替换% string格式化操作符。(但仍支持%运算符;它将在Python 3.1中被弃用,并在以后的某个时候从语言中删除。阅读PEP 3101获取完整的独家新闻。

>>> "hello {name}, how are you {name}, welcome {name}".format(name='john')
'hello john, how are you john, welcome john'

I prefer the first way since it is explicit, but here is a reason why .format is superior over % formatting

我喜欢第一种方式,因为它是显式的,但是这里有一个原因,为什么.format优于% formatting

>>> "hello {0}, how are you {0}, welcome {0}".format('john')
'hello john, how are you john, welcome john'

#3


3  

This works also:

这个作品也:

"hello %s , how are you %s, welcome %s"%tuple(["john"]*3)

or even shorter, without the explicit type cast:

甚至更短,没有明确的类型cast:

"hello %s , how are you %s, welcome %s"%(("john",)*3)

#4


1  

99% likely you should use .format()

99%可能你应该使用。format()

It's unlikely but if you had a series of greetings you could try this:

这不太可能,但如果你有一系列的问候,你可以试试这个:

>>> greetings = ["hello", "how are you", "welcome"]
>>> ", ".join(" ".join((greet, "John")) for greet in greetings)
'hello John, how are you John, welcome John'