python中的print
python3 中去除了print語句,加入print()函數實現相同的功能
print() 會在輸出窗口中顯示一些文本。
下面我們來介紹print中的內置方法sep和end
>>> help(print) Help on built-in function print in module builtins:print(...)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.sep—在字符串之間插入值,默認是一個空格,如:
# print中,每個字符串是用“,”逗號隔開的,默認是一個空格 >>> print('我是第一個字符串', '我是第二個字符串') 我是第一個字符串 我是第二個字符串# 如果我想讓兩個字符串中沒有空格,就可以使用sep了 >>> print('我是第一個字符串', '我是第二個字符串', sep='') 我是第一個字符串我是第二個字符串#當然,也可以使用其他的文字來隔開字符串。 >>> print('我是第一個字符串', '我是第二個字符串', sep='---我的存在,就是為了隔開你們---') 我是第一個字符串---我的存在,就是為了隔開你們---我是第二個字符串# 使用逗號來隔開字符串,為了效果明顯,估計多加了很多逗號~ >>> print('I', 'love', 'Python', sep=',,,,,') I,,,,,love,,,,,Python# \n是換行符 >>> print('我是第一個字符串', '我是第二個字符串', sep='\n') 我是第一個字符串 我是第二個字符串end—在字符串結尾追加一個值,默認是換行,如:
#輸入print點擊回車,就直接輸出了,如果使用分號,就可以在一行寫上兩個指令在輸出 >>> print('我在第一行!');print('我在第二行!') 我在第一行! 我在第二行!# 如果不加end的話,最后一個值默認是\n,但是如果加了end=‘’,就把\n給去掉了,變成一個空的字符串,所以兩個print就可以在一行顯示了~ >>> print('我在第一行!', end='');print('我在第二行!') 我在第一行!我在第二行!>>> print('我在第一行!', end='---就是不讓換行---');print('我在第二行!') 我在第一行!---就是不讓換行---我在第二行!如果我需要在一個字符串中嵌入一個雙引號,可以這樣做:
# 我們在"雙引號"中,加上"雙引號"的字符串,Python會以為這段話已經結束了,以為下個"雙引號"開始輸入下一段話,所以'它'會很善解人意的提示你“語法錯誤” >>> print("Python我非常喜歡"它",你們喜歡嗎?") SyntaxError: invalid syntax# 我們可以使用反斜杠來把"雙引號"給注釋掉 >>> print("Python我非常喜歡\"它\",你們喜歡嗎?") Python我非常喜歡"它",你們喜歡嗎?# 我們可以在'單引號'中輸入字符串,然后字符串中需要加"雙引號"的位置使用雙引號 >>> print('Python我非常喜歡"它",你們喜歡嗎?') Python我非常喜歡"它",你們喜歡嗎?# 當然,我們也可以在雙引號中,輸入單引號 >>> print("What's your name? \nMy name's Python") What's your name? My name's Pythonprint輸出精度控制
有時候print輸出需要控制精讀,則需要以下一些技巧:
有括號里的負號(-)的時候,輸出內容左對齊
沒有括號里的負號(-)的時候,輸出內容右對齊
第一個數字代表: 整數+小數的總位數
第二個數字代表:小數部分的位數
輸入 >>>’Python’與輸入 >>>print(‘Python’) 有何不同?
# 輸出結果是有引號的 >>> 'Python' 'Python'# 輸出結果沒有引號 >>> print('Python') Python在python中的計算,甚至可以不用print()函數,可以直接輸入數字計算
# 相加 >>> 5 + 8 13# 相減,并且輸出負數 >>> 5 - 8 -3# 相減, 輸出是正數 >>> 9 - 5 4# 相乘 >>> 2 * 3 6# 相除,輸出是個浮點型的小數 >>> 9 / 3 3.0# 如果想要輸出整數,需要用兩個除號“//” >>> 9 // 3 3還可以用來字符串的相加
>>> # 將字符串相加 >>> 'I' + 'love' + 'Python' 'IlovePython' >>> # 字符串相加后太丑了,我們在單詞后面加上空格 >>> 'I' + ' ' + 'love' + ' ' + 'Python' + '!' 'I love Python!' >>> # 中文與英文相加 >>> "我" + "愛" + "Python" '我愛Python'Python不僅就字符串拼接、相加這么簡單,還可以使用字符串相乘:
>>> 'I love Python! ' * 3 'I love Python! I love Python! I love Python! '但是其他的可能會報錯,比如字符串與數字相加,字符串與字符串相乘,字符串相減,字符串相除。如下
#字符串與數字相加 >>> 'I love Python' + 5 Traceback (most recent call last):File "<pyshell#7>", line 1, in <module>'I love Python' + 5 TypeError: Can't convert 'int' object to str implicitly#字符串與字符串相乘 >>> 'I love Python!' * 'love' Traceback (most recent call last):File "<pyshell#8>", line 1, in <module>'I love Python!' * 'love' TypeError: can't multiply sequence by non-int of type 'str'#字符串相減 >>> 'I love Python!' - 'love' Traceback (most recent call last):File "<pyshell#9>", line 1, in <module>'I love Python!' - 'love' TypeError: unsupported operand type(s) for -: 'str' and 'str'#字符串相除 >>> 'I love Python!' / 3 Traceback (most recent call last):File "<pyshell#10>", line 1, in <module>'I love Python!' / 3 TypeError: unsupported operand type(s) for /: 'str' and 'int'print打印控制臺改變輸出字體顏色或者字體背景
命令格式
格式:\033[顯示方式;前景色;背景色m說明: 前景色 背景色 顏色 --------------------------------------- 30 40 黑色 31 41 紅色 32 42 綠色 33 43 黃色 34 44 藍色 35 45 紫紅色 36 46 青藍色 37 47 白色 顯示方式 意義 ---------------------------------------- 0 終端默認設置 1 高亮顯示 4 使用下劃線 5 閃爍 7 反白顯示 8 不可見 print('\033[1;32;45m',__doc__)更多細節請直接查看博文
http://www.cnblogs.com/yyhh/p/4202829.html?utm_source=tuicool&utm_medium=referral
http://www.codingpy.com/article/why-print-became-a-function-in-python-3/
Python學習筆記(9)–print輸出不同顏色
總結
以上是生活随笔為你收集整理的python中的print的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平安银行信用卡面签好过吗?通过率怎么样?
- 下一篇: 信用卡冻结了还能刷吗?信用卡冻结了如何解