Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:36:08+00:00 2026-05-25T11:36:08+00:00

I am trying to split a Unicode string into words (simplistic), like this: print

  • 0

I am trying to split a Unicode string into words (simplistic), like this:

print re.findall(r'(?u)\w+', "раз два три")

What I expect to see is:

['раз','два','три']

But what I really get is:

['\xd1', '\xd0', '\xd0', '\xd0', '\xd0\xb2\xd0', '\xd1', '\xd1', '\xd0']

What am I doing wrong?

Edit:

If I use u in front of the string:

print re.findall(r'(?u)\w+', u"раз два три")

I get:

[u'\u0440\u0430\u0437', u'\u0434\u0432\u0430', u'\u0442\u0440\u0438']

Edit 2:

Aaaaand it seems like I should have read docs first:

 print re.findall(r'(?u)\w+', u"раз два три")[0].encode('utf-8')

Will give me:

раз

Just to make sure though, does that sound like a proper way of approaching it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:36:09+00:00Added an answer on May 25, 2026 at 11:36 am

    You’re actually getting the stuff you expect in the unicode case. You only think you are not because of the weird escaping due to the fact that you’re looking at the reprs of the strings, not not printing their unescaped values. (This is just how lists are displayed.)

    >>> words = [u'\u0440\u0430\u0437', u'\u0434\u0432\u0430', u'\u0442\u0440\u0438'] 
    >>> for w in words:
    ...     print w # This uses the terminal encoding -- _only_ utilize interactively
    ... 
    раз
    два
    три
    >>> u'раз' == u'\u0440\u0430\u0437'
    True
    

    Don’t miss my remark about printing these unicode strings. Normally if you were going to send them to screen, a file, over the wire, etc. you need to manually encode them into the correct encoding. When you use print, Python tries to leverage your terminal’s encoding, but it can only do that if there is a terminal. Because you don’t generally know if there is one, you should only rely on this in the interactive interpreter, and always encode to the right encoding explicitly otherwise.

    In this simple splitting-on-whitespace approach, you might not want to use regex at all but simply to use the unicode.split method.

    >>> u"раз два три".split()
    [u'\u0440\u0430\u0437', u'\u0434\u0432\u0430', u'\u0442\u0440\u0438']
    

    Your top (bytestring) example does not work because re basically assumes all bytestrings are ASCII for its semantics, but yours was not. Using unicode strings allows you to get the right semantics for your alphabet and locale. As much as possible, textual data should always be represented using unicode rather than str.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to split a string up into words and punctuation, adding the punctuation
I'm trying to split a string into an array of words, however I want
I'm trying to split a string using the method found in this thread ,
I’m trying to split a String . Simple examples work: groovy:000> print abc,def.split(,); [abc,
I am trying to split a string into an array of word pairs in
Im trying to split a string according to the following rules: words without around
I am trying to split a text into an array using explode, but for
I've been trying to split a name into first and last name, but I'm
I'm trying to split a string: 'QH QD JC KD JS' into a list
I'm trying to split a string formatted like Bananas|,|Bananas|||Bananas|Oranges|,|Bananas|||Bananas|Oranges|||Bananas|Oranges|Green Apples|,|Bananas|||Bananas|Oranges|||Bananas|Oranges|Red Apples|,|Bananas|||Bananas|Oranges|||Bananas|Oranges|Pears with a regex,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.