python (三) Python 字符串

2017-02-02  本文已影响31人  StarShift

hello word

开始编写第一个python程序,通常python 程序需要有

#!/usr/bin/python

if __name__ == "__main__":
    print "hello word"

Python 中的常量

Python 中的数

Python 中的字符串

Python 中的文档字符串
Python有一个很奇妙的特性,称为 文档字符串,它通常被简称为 docstrings 。DocStrings是一个重要的工具,由于它帮助你的程序文档更加简单易懂,你应该尽量使用它。甚至可以通过Python中的DocString来自动生成文档。
在函数的第一个逻辑行的字符串是这个函数的 文档字符串 。注意,DocStrings也适用于模块和类。
文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。 强烈建议 你在你的函数中使用文档字符串时遵循这个惯例。
你可以使用doc(注意双下划线)调用printMax函数的文档字符串属性(属于函数的名称)。
自动化工具也可以以同样的方式从你的程序中提取文档。因此,我 强烈建议 你对你所写的任何正式函数编写文档字符串。随你的Python发行版附带的pydoc命令,与help()类似地使用DocStrings。
更多关于DocString的内容请阅读:https://www.python.org/dev/peps/pep-0257/

#!/usr/bin/python  
# Filename: func_doc.py  
  
def printMax(x, y):  
    '''''Prints the maximum of two numbers. 
 
    The two values must be integers.'''  
    x = int(x) # convert to integers, if possible  
    y = int(y)  
  
    if x > y:  
        print x, 'is maximum'  
    else:  
        print y, 'is maximum'  
  
printMax(3, 5)  
print printMax.__doc__  
#!/usr/bin/env python
# *coding=utf-8
string = 'this is ''a string '
print string

将会输出

this is a string
上一篇 下一篇

猜你喜欢

热点阅读