python多行注釋和跨行字元串
- 2020 年 1 月 7 日
- 筆記
1》單行注釋:# 如:
#hello python
2》多行注釋:三個單引號(或三個雙引號),如:
'''hello python
hello world'''
或
"""hello python
hello world"""
3》三個單引號(或三個雙引號)也可以表示跨行字元串,如: >>> s=''' … hello … python … ''' >>> s 'nhellonpythonn'
>>> print s
hello
python
>>>
4》跨行字元串可以用來進行格式化輸出,下面是一個使用跨行字元串進行格式化輸出的例子:
song@ubuntu:~$ vi ex2.py song@ubuntu:~$ more ex2.py #!/usr/bin/env python #_*_ coding:utf-8 _*_ name=raw_input('Please input your name:') age=raw_input('Please input your age:') job=raw_input('Please input your job:') sex=raw_input('Please input your sex:') print '''Personnal information of %s: name:%s age:%s job:%s sex:%s ''' % (name,name,age,job,sex) song@ubuntu:~$ python ex2.py Please input your name:python Please input your age:18 Please input your job:IT Please input your sex:m Personnal information of python: name:python age:18 job:IT sex:m
song@ubuntu:~$