Skip to main content

A library to parse or validate Twitter texts properly

Project description


Documentation Status

This is a Python port of the twitter/twitter-text libraries, fully compliant with the official conformance test suite.


This library calculates length of a tweet message according to the documentation from Twitter Developers, so that you can validate the tweet without calling the Web API at all. Although counting characters might seem an easy task, in actual fact it is very complicated, especially when the text contains CJK characters, URLs, or emojis.

The original twitter-text libraries have hit-highlighting and auto-linking features as well, however they are not yet supported by this Python port.



$ pip install twitter-text-parser


See the API reference for more details.

from twitter_text import parse_tweet, extract_emojis_with_indices, extract_urls_with_indices

text = 'english text 日本語 😷'

assert parse_tweet(text).asdict() == {
    'weightedLength': 46,
    'valid': True,
    'permillage': 164,
    'validRangeStart': 0,
    'validRangeEnd': 38,
    'displayRangeStart': 0,
    'displayRangeEnd': 38

assert extract_urls_with_indices(text) == [{
    'url': '',
    'indices': [19, 38]

assert extract_emojis_with_indices(text) == [{
    'emoji': '😷',
    'indices': [17, 18]

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitter_text_parser-3.0.0.tar.gz (61.7 kB view hashes)

Uploaded source

Built Distribution

twitter_text_parser-3.0.0-py3-none-any.whl (69.9 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page