我需要写入前一行的命令,比如没有\n的print().
这是一些示例代码:
a=0
print("Random string value")
if a==0:
print_to_previous_line("is random")
Run Code Online (Sandbox Code Playgroud)
和输出
Random string value is random
Run Code Online (Sandbox Code Playgroud)
我知道我可以像print("string",value)那样在同一个打印命令中使用多个不同的东西,但这不是答案.理由太乱了,不能在这里解释,但这种"打印到上一行"将是完全正确的答案.那么它在现实中会是什么?
我在Python中有这个代码
inputted = input("Enter in something: ")
print("Input is {0}, including the return".format(inputted))
Run Code Online (Sandbox Code Playgroud)
那个输出
Enter in something: something
Input is something
, including the return
Run Code Online (Sandbox Code Playgroud)
我不确定发生了什么; 如果我使用不依赖于用户输入的变量,我在使用变量格式化后不会获得换行符.我怀疑当我点击返回时,Python可能会将换行符作为输入.
我怎样才能使输入不包含任何换行符,以便我可以将它与其他字符串/字符进行比较?(例如something == 'a'
)
我有类似的东西: print "\n","|",id,"|",var1,"|",var2,"|",var3,"|",var4,"|"
它为每个变量打印空格.
| 1 | john | h | johnny | mba |
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
|1|john|h|johnny|mba|
Run Code Online (Sandbox Code Playgroud)
我有20个变量,我必须打印,我讨厌为每个变量使用sys.stdout.write(var).谢谢Pythonistas!
我使用python 2.6并阅读了许多关于从'print'中删除新行的链接,但是找不到使用示例以及使用模数符号(%)进行格式化.在我的程序中,我试图在计算数据的循环线中写入,但每个行数据来自不同的计算:
while loop ... calulating value1 and value2 print ('%10d %10s') % (value1, value2) [1] ... calulating value3 and value4 print ('%7s %15d') % (value3, value4) [2] print #this is where newline should come from
所以我想得到:
value1 value2 value3 value4 value5 value6 value7 value8 ...
基本上这种方法保持了我的程序的可读性(每个实际行具有超过20个计算位置).相反的方法是将所有数据连接成一个长字符串,但可读性可能会丢失.
是否可以使用[1]和[2]中的"print()%()"语法删除换行符?
我有一个代码如下:
print 'Going to %s'%href
try:
self.opener.open(self.url+href)
print 'OK'
Run Code Online (Sandbox Code Playgroud)
当我执行它时,我明显得到两行:
Going to mysite.php
OK
Run Code Online (Sandbox Code Playgroud)
但希望是:
Going to mysite.php OK
Run Code Online (Sandbox Code Playgroud) 我无法删除for
-loop 中的间距,因为数字不是用来制作图案的.
我的代码:
for i in range(1,5):
for j in range(1,i):
print(j)
Run Code Online (Sandbox Code Playgroud)
但我想要的输出是:
1
12
123
1234
Run Code Online (Sandbox Code Playgroud) 我是python的初学者.我想在控制台中运行这个程序员,但它告诉我错误是什么错误.
>>> def fib(n):
... a, b = 0, 1
... while a < n:
... print (a, end=' ')
File "<stdin>", line 4
print (a, end=' ')
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我的实际程序,我想运行:
>>> def fib(n):
... a, b = 0, 1
... while a < n:
... print(a, end=' ')
... a, b = b, a+b
... print()
>>> fib(1000)
Run Code Online (Sandbox Code Playgroud) 我写了这个程序:
for i in range(1,6):
for j in range(65,65+i):
a = chr(j)
print (a)
print
Run Code Online (Sandbox Code Playgroud)
我想打印一个模式如下:
A
A B
A B C
A B C D
A B C D E
Run Code Online (Sandbox Code Playgroud)
但我没有得到所需的输出
我正进入(状态
A
A
B
A
B
C
A
B
C
D
A
B
C
D
E
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
num = input('Amount of numbers:')
num = int(num)
for x in range(1, num + 1):
if x == 1:
print('1st')
elif x == 2:
print('2nd')
elif x == 3:
print('3rd')
elif x >= 4:
print(x, 'th')
Run Code Online (Sandbox Code Playgroud)
这是输出(样本):
Amount of numbers:8
1st
2nd
3rd
4 th
5 th
6 th
7 th
8 th
Run Code Online (Sandbox Code Playgroud)
如您所见,4 之后的所有数字在它和“th”之间都有一个空格。我该如何解决?
我一直在使用该功能,sys.stdout.write(string)
但我想知道是否有另一种方法用于此目的.提前致谢!
python ×10
python-2.7 ×2
function ×1
input ×1
newline ×1
printing ×1
python-2.x ×1
python-3.x ×1
stdout ×1
string ×1
user-input ×1