Discuss / Python / 求解

求解

Topic source

name = input() print('hello,',name)

C:\Workspace> python hello.py Michael hello, Michael

第一段是文件,第二段是在命令行里运行文件,我想的是,我怎么可以把M前的空格去掉???

需要去换字体,大概用这个方法吧

In [3]: print?
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
Type:      builtin_function_or_method

print函数默认的分隔符是' ' 要去掉M前边的空格有几种方法,

1.不给print插入分隔符的机会 print('hello,%s' % name) 2.指定默认的分隔符为'',注意 :虽然这两个单引号中间劈得很宽,像是有半个空格的样子,其实它俩中间真的没有其他字符混进去. print('hello,',name,sep='')

看到别人有个比较方便的方法,就这样

print('Hello,'+name)

哈哈,楼上的姑娘你赢了

print('Hello,'+name) #这样写确实更机智

我娘子1

#6 Created at ... [Delete] [Delete and Lock User]

去掉"hello,"后面的逗号就是了,因为逗号就是代表空格

MissMiami

#7 Created at ... [Delete] [Delete and Lock User]

@我娘子1 直接去掉逗号的话是会报错的,会提示语法错误。

iptables

#8 Created at ... [Delete] [Delete and Lock User]

print('hello,'+name) 就可以了,字符串拼接

ACCTWatson

#9 Created at ... [Delete] [Delete and Lock User]

thus


  • 1

Reply