Ruby示例:
name = "Spongebob Squarepants"
puts "Who lives in a Pineapple under the sea? \n#{name}."
Run Code Online (Sandbox Code Playgroud)
成功的Python字符串连接对我来说似乎很冗长.
我想int加入一个string.这就是我现在正在做的事情:
num = 40
plot.savefig('hanning40.pdf') #problem line
Run Code Online (Sandbox Code Playgroud)
我必须运行几个不同数字的程序,而不是两个40.所以我想做一个循环但插入这样的变量不起作用:
plot.savefig('hanning', num, '.pdf')
Run Code Online (Sandbox Code Playgroud)
如何将变量插入Python字符串?
该程序应该采用两个名称,如果它们的长度相同,则应检查它们是否是同一个单词.如果它是相同的单词,它将打印"名称相同".如果它们长度相同但字母不同,则会打印"名称不同但长度相同".我遇到问题的部分是在底部4行.
#!/usr/bin/env python
# Enter your code for "What's In (The Length Of) A Name?" here.
name1 = input("Enter name 1: ")
name2 = input("Enter name 2: ")
len(name1)
len(name2)
if len(name1) == len(name2):
if name1 == name2:
print ("The names are the same")
else:
print ("The names are different, but are the same length")
if len(name1) > len(name2):
print ("'{0}' is longer than '{1}'"% name1, name2)
elif len(name1) < len(name2):
print ("'{0}'is longer than '{1}'"% …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像的字符串,'%s in %s'我想知道如何分隔参数,使它们是两个不同的%s.我的思绪来自Java想出了这个:
'%s in %s' % unicode(self.author), unicode(self.publication)
Run Code Online (Sandbox Code Playgroud)
但这不起作用,所以它在Python中看起来如何?
Python至少有六种格式化字符串的方法:
In [1]: world = "Earth"
# method 1a
In [2]: "Hello, %s" % world
Out[2]: 'Hello, Earth'
# method 1b
In [3]: "Hello, %(planet)s" % {"planet": world}
Out[3]: 'Hello, Earth'
# method 2a
In [4]: "Hello, {0}".format(world)
Out[4]: 'Hello, Earth'
# method 2b
In [5]: "Hello, {planet}".format(planet=world)
Out[5]: 'Hello, Earth'
# method 2c
In [6]: f"Hello, {world}"
Out[6]: 'Hello, Earth'
In [7]: from string import Template
# method 3
In [8]: Template("Hello, $planet").substitute(planet=world)
Out[8]: 'Hello, Earth'
Run Code Online (Sandbox Code Playgroud)
不同方法的简要历史:
printf自Pythons婴儿期以来,风格格式一直存在 …python printf string-formatting backwards-compatibility deprecated
在Python中,似乎有两种不同的生成格式化输出的方法:
user = "Alex"
number = 38746
print("%s asked %d questions on stackoverflow.com" % (user, number))
print("{0} asked {1} questions on stackoverflow.com".format(user, number))
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以优先于另一种方式?它们是等价的,有什么区别?应该使用什么形式,特别是对于Python3?
如果我尝试执行以下操作:
things = 5
print("You have " + things + " things.")
Run Code Online (Sandbox Code Playgroud)
我在Python 3.x中收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)
......以及Python 2.x中的类似错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我需要从随机整数列表(0-255)创建一个十六进制数字字符串.每个十六进制数字应由两个字符表示:5 - "05",16 - "10"等.
例:
Run Code Online (Sandbox Code Playgroud)Input: [0,1,2,3,127,200,255], Output: 000102037fc8ff
我设法提出:
#!/usr/bin/env python
def format_me(nums):
result = ""
for i in nums:
if i <= 9:
result += "0%x" % i
else:
result += "%x" % i
return result
print format_me([0,1,2,3,127,200,255])
Run Code Online (Sandbox Code Playgroud)
但是,这看起来有点尴尬.有更简单的方法吗?
我试图printf()在Linux上的python命令行中使用C函数.为了完成这项工作,我导入了ctypes.我的问题是:如果我创建一个在循环中CDLL使用printf()-function 的对象,我得到一个非常奇怪的输出:
>>> import ctypes
>>> libc = ctypes.CDLL("libc.so.6")
>>> for i in range(10):
... libc.printf("%d", i)
...
01
11
21
31
41
51
61
71
81
91
>>>
Run Code Online (Sandbox Code Playgroud)
但是,当我在函数内部调用此循环时,它按预期工作:
>>> import ctypes
>>> libc = ctypes.CDLL("libc.so.6")
>>> def pr():
... for i in range(10):
... libc.printf("%d", i)
... libc.printf("\n")
...
>>> pr()
0123456789
>>>
Run Code Online (Sandbox Code Playgroud)
我猜不出是什么导致了这种行为......
如果重要的话,我在Linux上使用Python 2.7.6.
编辑:
Python版本/操作系统对此没有影响.有关详细信息,请参阅下面的PM 2Ring的答案.在Windows上,您只需将初始化更改libc为libc = ctypes.CDLL("msvcrt.dll")where .dll是可选的.获取正确输出比调用函数的另一种方法是将printf()变量值存储在变量中:
>>> …Run Code Online (Sandbox Code Playgroud) python ×10
string ×4
python-3.x ×2
ctypes ×1
deprecated ×1
format ×1
formatting ×1
printf ×1
printing ×1
python-2.7 ×1
python-2.x ×1
syntax ×1
typeerror ×1
variables ×1