Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在同一行上获取两个文本字符串?_Python_String_Text - Fatal编程技术网

Python 在同一行上获取两个文本字符串?

Python 在同一行上获取两个文本字符串?,python,string,text,Python,String,Text,为这个糟糕的标题感到抱歉!我不知道该写什么,因为我没有把这张瓷砖做成500字长 假设我有 print 'Hi!' print 'How are you?' 有没有办法让我把这件事放在同一条线上?你好你好吗 以下是我目前使用的代码: choice = raw_input('>: ') if choice=='1': menu1() else: print '' +choice print 'is not a recognized commando.' 我的另一个代

为这个糟糕的标题感到抱歉!我不知道该写什么,因为我没有把这张瓷砖做成500字长

假设我有

print 'Hi!'
print 'How are you?'
有没有办法让我把这件事放在同一条线上?你好你好吗

以下是我目前使用的代码:

choice = raw_input('>: ')
if choice=='1':
    menu1()
else:
    print '' +choice
    print 'is not a recognized commando.'
我的另一个代码是:

from colorama import Fore, Back, Style
print
print 'Now chatting with Tom'
time.sleep(2)
print
print
print(Fore.RED + 'Tom >: ') + print(Fore.GREEN + 'test')
当然,这不起作用。我只是想测试一下

有没有办法把这两个字符串放在同一行

多谢各位

编辑:

非常感谢大家!我知道Python最基本的部分,但我不知道如何忽略

无论如何。由于某种原因,我得到了额外的空间。我想写[12:41:39]时间。在我的代码中,它如下所示:

print(Fore.YELLOW + '['),
print strftime("%H:%M:%S"),
print ']          ',
输出为[12:41:39]

我不知道这里出了什么问题。我真希望这里有人能给我解释一下!谢谢大家!

在蟒蛇3中:

print('text', end='')
print('text', end='')
我想这就是你要找的!:文本

您还可以设置分隔符:

print('text', 'text', 'text', sep = '')

给出:text而不是text

使用尾随逗号,这将抑制打印后添加的换行符。退房:


在语句末尾添加逗号:

print 'Hi!',
print 'How are you?'
输出:

Hi! How are you?
这是来自:

除非print语句以逗号结尾,否则结尾将写入“\n”字符


在Python2.x中,可以在print语句后使用逗号

print 'Hi!',
print 'How are you?'
这将输出:

Hi! How are you?     
您还可以从Python3.x导入print函数并执行以下操作

from __future__ import print_function
print("Hi!", end='')

这会将空字符串作为结束字符,而不是\n

您也可以将所有字符串放在一个字符串中,如下所示:

>>> string = 'abc' + 'def'
>>> print string
'abcdef'
使用逗号:

打印“嗨!”“你好吗?”

在Python2.x中,添加一个尾随逗号:

print 'Hi!',  # No newline will be printed
在Python 3.x中,此处不是语句,而是为end关键字参数传递一个空字符串:

print('Hi!', end='')

如果在Python 2.x代码中使用from uuu future uuu导入print u函数以实现向前兼容性,则需要使用函数版本而不是语句版本。

您使用的是Python 2或3?只是猜测一下,但是看起来他在使用Python2.x,因为他的打印没有括号。我在使用Python2.7,它比Python3更容易使用。只是猜测一下,但看起来他在使用Python2.x,因为他的打印没有括号。谢谢你提供的信息!正如thegrinner所说,我正在使用Python2.7,因为我觉得它比Python3更方便。出于某种原因,当我这样做时,我会得到额外的空间。我已经更新了我的帖子,在那里你可以看到我的新代码,希望你能找出错误所在。非常感谢。
print('Hi!', end='')