Skip to content

Instantly share code, notes, and snippets.

@yubessy
Created June 29, 2014 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yubessy/3b0e0302e8afa894950b to your computer and use it in GitHub Desktop.
Save yubessy/3b0e0302e8afa894950b to your computer and use it in GitHub Desktop.
長い文字列を短く表示
# -*- coding: utf-8 -*-
def shorten(s, length=80):
u"""
長い文字列を"abraca ... tabra"のように短く表示
"""
if len(s) < length:
return s
else:
if isinstance(s, unicode):
sp = u' ... '
else:
sp = ' ... '
return s[:length / 2 - 2] + sp + s[- length / 2 + 3:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment