Python编程之求字符串长度

问题描述:写一个函数,求一个字符串的长度,在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.

please input a string:
abcef
the string has 5 characters.

please input a string:
abchgby
the string has 7 characters.

please input a string:
nhbygtrscv
the string has 10 characters.

 

你可能感兴趣的:(Python学习馆,Python学习馆)