Python 練習實例70

Python 100例 Python 100例

題目:寫一個函數,求一個字串的長度,在main函數中輸入字串,並輸出其長度。

程式分析:無。

程式源代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

if __name__ == '__main__':
    s = raw_input('please input a string:\n')
    print 'the string has %d characters.' % len(s)

以上實例輸出結果為:

please input a string:
abc
the string has 3 characters.

Python 100例 Python 100例