Python 2.6引入了该str.format()方法,其语法与现有%运算符略有不同.哪种情况更好,哪种情况更好?
以下使用每种方法并具有相同的结果,那么有什么区别?
#!/usr/bin/python
sub1 = "python string!"
sub2 = "an arg"
a = "i am a %s" % sub1
b = "i am a {0}".format(sub1)
c = "with %(kwarg)s!" % {'kwarg':sub2}
d = "with {kwarg}!".format(kwarg=sub2)
print a # "i am a python string!"
print b # "i am a python string!"
print c # "with an arg!"
print d # "with an arg!"
Run Code Online (Sandbox Code Playgroud)此外,何时在Python中发生字符串格式化?例如,如果我的日志记录级别设置为HIGH,我仍然会执行以下%操作吗?如果是这样,有没有办法避免这种情况?
log.debug("some debug info: %s" % some_info)
Run Code Online (Sandbox Code Playgroud)我想在for循环中使用附加到它的整数创建字符串.像这样:
for i in range(1,11):
string="string"+i
Run Code Online (Sandbox Code Playgroud)
但它返回一个错误:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Run Code Online (Sandbox Code Playgroud)
连接String和Integer的最佳方法是什么?
我想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字符串?
我有这个python程序,它将字符串添加到整数:
a = raw_input("Enter a: ")
b = raw_input("Enter b: ")
print "a + b as strings: " + a + b
a = int(a)
b = int(b)
c = a + b
str(c)
print "a + b as integers: " + c
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Python: TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
如何将字符串添加到整数?
我正在尝试编写一个文本游戏,我在我定义的函数中遇到了一个错误,让你在制作角色后基本上可以花费你的技能点数.起初,错误表明我试图从代码的这一部分中的整数中减去一个字符串:balance - strength.显然这是错的,所以我用strength = int(strength)... 修复它但是现在我得到了这个我以前从未见过的错误(新程序员)而且我很难说它到底想要告诉我什么以及我如何修复它.
这是我的函数部分无法正常工作的代码:
def attributeSelection():
balance = 25
print("Your SP balance is currently 25.")
strength = input("How much SP do you want to put into strength?")
strength = int(strength)
balanceAfterStrength = balance - strength
if balanceAfterStrength == 0:
print("Your SP balance is now 0.")
attributeConfirmation()
elif strength < 0:
print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
attributeSelection()
elif strength > balance:
print("That is …Run Code Online (Sandbox Code Playgroud) 我试图在Python中连接字符串和数字.当我尝试这个时它给了我一个错误:
"abc" + 9
Run Code Online (Sandbox Code Playgroud)
错误是:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
"abc" + 9
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
为什么我无法做到这一点?
如何可以我将字符串和Python中的号码是多少?
我尝试取一个整数并在其前面添加"b",将其转换为字符串时出现此错误:
File "program.py", line 19, in getname
name = "b" + num
TypeError: Can't convert 'int' object to str implicitly
Run Code Online (Sandbox Code Playgroud)
这与此功能有关:
num = random.randint(1,25)
name = "b" + num
Run Code Online (Sandbox Code Playgroud) 我们的几何老师给了我们一个任务,要求我们创建一个玩具在现实生活中使用几何体的例子,所以我认为制作一个程序来计算需要多少加仑的水来填充某个池才是很酷的.形状,并具有一定的尺寸.
这是迄今为止的计划:
import easygui
easygui.msgbox("This program will help determine how many gallons will be needed to fill up a pool based off of the dimensions given.")
pool=easygui.buttonbox("What is the shape of the pool?",
choices=['square/rectangle','circle'])
if pool=='circle':
height=easygui.enterbox("How deep is the pool?")
radius=easygui.enterbox("What is the distance between the edge of the pool and the center of the pool (radius)?")
easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
Run Code Online (Sandbox Code Playgroud)
我不断得到这个错误:easygui.msgbox =("你需要"+(3.14*(浮动(半径)**2)*浮动(高度))+"加仑水来填充这个池.")TypeError:不能连接'str'和'float'对象
我该怎么办?
我目前正在学习Python,所以我不知道发生了什么.
num1 = int(input("What is your first number? "))
num2 = int(input("What is your second number? "))
num3 = int(input("What is your third number? "))
numlist = [num1, num2, num3]
print(numlist)
print("Now I will remove the 3rd number")
print(numlist.pop(2) + " has been removed")
print("The list now looks like " + str(numlist))
Run Code Online (Sandbox Code Playgroud)
当我运行程序,输入num1,num2和num3的数字时,它返回:Traceback(最近一次调用最后一次):
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Run Code Online (Sandbox Code Playgroud) 在Python中,以下代码会产生错误:
a = 'abc'
b = 1
print(a + b)
Run Code Online (Sandbox Code Playgroud)
(错误是"TypeError:无法连接'str'和'int'对象").
为什么Python解释器在遇到这些类型的串联时不会自动尝试使用str()函数?