我目前正在学习Python,并编写了一个程序来试验这门语言.但是,每当我使用它时,输出总是在其中有一个字母"u".我正在使用Pyscripter作为我的IDE.
这是我的代码:
print "whats your name"
age = raw_input()
print "Alright, so %r, I just realized what percent-r does actually or is meant for" % (age)
print "What next ur age",
age1 = raw_input()
print "you entered %r " % (age1)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我看到这样的事情:
>>> Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32.
>>> whats your name (i typed kk)
>>> Alright, so u'kk', i just realized what percent-r does actually or is meant for
>>> …Run Code Online (Sandbox Code Playgroud) 我收到这个错误,我不明白为什么.
我正在学习使用Gmail的API并复制粘贴他们的示例:https://developers.google.com/gmail/api/v1/reference/users/threads/get#examples
这是代码:
def GetThread(service, user_id, thread_id):
"""Get a Thread.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
thread_id: The ID of the Thread required.
Returns:
Thread with matching ID.
"""
try:
thread = service.users().threads().get(userId=user_id, id=thread_id).execute()
messages = thread['messages']
print ('thread id: %s - number of messages '
'in this thread: %d') % (thread['id'], len(messages))
return thread
except errors.HttpError, error:
print 'An …Run Code Online (Sandbox Code Playgroud) 我在Python中遇到过这样的行:
print "Let's talk about %s." % my_name
Run Code Online (Sandbox Code Playgroud)
但是,我在Ruby中看到了这两个具有相同输出的情况:
print "Let's talk about #{my_name}."
print "Let's talk about %s." % my_name"
是否只有一种方法可以在Python中打印相同的行?提前致谢!
我使用python 3.4,我可以用两种方式格式化字符串:
print("%d %d" %(1, 2))
Run Code Online (Sandbox Code Playgroud)
和
print("{:d} {:d}".format(1, 2))
Run Code Online (Sandbox Code Playgroud)
在文档中,他们仅使用"格式"显示示例.这是否意味着使用'%'并不好,或者使用哪个版本并不重要?
我正在为 python 3.x 使用 MySQL 连接器,并且使用简单的代码遇到了这个问题,例如:
rest_cursor.execute("SELECT * FROM restaurant WHERE r_id=%s",(r_id))
其中 r_id 是一个整数。这导致抛出异常:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1
这种格式似乎与 python-MySQL 示例页面中的示例相匹配:http : //dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html。
我究竟做错了什么?谢谢
大多数情况下,我使用Python3。我可以写这个吗
print(f"the answer is {21 + 21} !")
Run Code Online (Sandbox Code Playgroud)
输出:答案是42!但是在Python 2中f字符串不存在。那么这是最好的方法吗?
print("the answer is " + str(21 + 21) + "!")
Run Code Online (Sandbox Code Playgroud) 我是 python 新手,需要一些帮助。
我有一个变量
q = request.GET['q']
Run Code Online (Sandbox Code Playgroud)
如何在其中插入变量q:
url = "http://search.com/search?term="+q+"&location=sf"
Run Code Online (Sandbox Code Playgroud)
现在我不确定公约是什么?我习惯了 PHP 或 javascript,但我正在学习 python,如何动态插入变量?
我有一个字符串:
testString = """ My name is %s
and I am %s years old
and I live in %s"""
Run Code Online (Sandbox Code Playgroud)
我有代码可以找到我想输入到 testString 中的这三个字符串。我怎么做?在 Java 中,我会这样做:
return String.format(testString, string1, string2, string3)
Run Code Online (Sandbox Code Playgroud)
Python中是否有等价物?