python print 如何避免输出空格,产生新的一行
避免输出空格
print "%s%s%s%s" % ('a','s','d','f')
或者
print 'me'+'no'+'likee'+'spacees'+'pls'
或者
import sys
print "hello",
sys.stdout.softspace=0
print "world",
输出结果为 helloworld ,中间没有空格
参考:
http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-spaces
>>> sentence = 'hello apple'
>>> sentence.strip() #去除开头和末尾的空格#
'hello apple'
>>> sentence = ''.join(sentence.split())#去除所有的空格#
>>> sentence
'helloapple'
或者用replace
string.replace(" ", "")
参考:
http://stackoverflow.com/questions/8270092/python-remove-whitespaces-in-string
删除一个列表中的特定字符串:
list.remove("a") # 只能删除一个
参考:
http://stackoverflow.com/questions/3845423/remove-empty-strings-from-a-list-of-strings
避免输出新行
print "this should be", #最后用逗号分隔
print "on the same line"
参考:
http://stackoverflow.com/questions/11266068/python-avoid-new-line-with-print-command
print "%s%s%s%s" % ('a','s','d','f')
或者
print 'me'+'no'+'likee'+'spacees'+'pls'
或者
import sys
print "hello",
sys.stdout.softspace=0
print "world",
输出结果为 helloworld ,中间没有空格
参考:
http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-spaces
>>> sentence = 'hello apple'
>>> sentence.strip() #去除开头和末尾的空格#
'hello apple'
>>> sentence = ''.join(sentence.split())#去除所有的空格#
>>> sentence
'helloapple'
或者用replace
string.replace(" ", "")
参考:
http://stackoverflow.com/questions/8270092/python-remove-whitespaces-in-string
删除一个列表中的特定字符串:
list.remove("a") # 只能删除一个
参考:
http://stackoverflow.com/questions/3845423/remove-empty-strings-from-a-list-of-strings
避免输出新行
print "this should be", #最后用逗号分隔
print "on the same line"
参考:
http://stackoverflow.com/questions/11266068/python-avoid-new-line-with-print-command