Skip to content

Instantly share code, notes, and snippets.

@zhasm
Created May 5, 2011 00:26
Show Gist options
  • Save zhasm/956308 to your computer and use it in GitHub Desktop.
Save zhasm/956308 to your computer and use it in GitHub Desktop.
Get the length of the given string, which can be ascii string or unicode or both
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename len.py
#created: 2011-04-29 15:18
import sys
def Len(s):
'''Get the length of the given string, which can be ascii string or unicode or both'''
s=s.decode('utf8')
return len(s)
def main():
'''
get input from standard input or pipe.
args can be seperated by consecutive white space, which will be trimed to single space.
if you need to count length of the space, wrap the whole string with quotation mark.
'''
if len(sys.argv) > 1:
sentence=" ".join(sys.argv[1:])
else:
sentence=raw_input()
print Len(sentence)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment