What's the u prefix in a Python string?
Like in:
u'Hello'
My guess is that it indicates "Unicode", is it correct?
If so, since when is it available?
python syntax
add a comment |
Like in:
u'Hello'
My guess is that it indicates "Unicode", is it correct?
If so, since when is it available?
python syntax
34
@S.Lott, Well is not always easy to spot the reference in the exact page and line, specially when you're new to a certain language. I find easier and faster to ask here. ( See: meta.stackexchange.com/questions/16353/… ) Btw, It's amazing how quick does google indexed this thread, it is now the third result: google.com/search?q=u+prefix+on+python
– OscarRyz
Mar 17 '10 at 19:02
1
@S.Lott: You're right. And now I know where that language reference is :)
– OscarRyz
Mar 17 '10 at 19:29
12
@OscarRyz is right. I made a google search for this 3 years later... and it got me to the answer with a sonic BOOM!
– James Bedford
Mar 29 '13 at 11:18
17
@JamesBedford, agreed. a search for "python u" came up with this result, which was just what I needed. The Python docs are great for the details, but locating something within them is a true, royal pain. A search for "u" on the python docs page returns a whole host of useless material. People who like to spout "read the docs" are ridiculously annoying, since searching the docs for something is so damn inefficient in the first place. If they weren't so bad at providing relevant results, that advice might be good.
– Josh Brown
Mar 19 '14 at 18:33
2
@S.Lott Google is my first resort. It found this page, which has the answer. Stackoverflow is far more reliable and understandable than any docs.
– denson
Jan 5 '17 at 15:40
add a comment |
Like in:
u'Hello'
My guess is that it indicates "Unicode", is it correct?
If so, since when is it available?
python syntax
Like in:
u'Hello'
My guess is that it indicates "Unicode", is it correct?
If so, since when is it available?
python syntax
python syntax
edited Oct 9 '17 at 18:51
Peter Mortensen
13.7k1986113
13.7k1986113
asked Mar 17 '10 at 18:43
OscarRyzOscarRyz
143k99338518
143k99338518
34
@S.Lott, Well is not always easy to spot the reference in the exact page and line, specially when you're new to a certain language. I find easier and faster to ask here. ( See: meta.stackexchange.com/questions/16353/… ) Btw, It's amazing how quick does google indexed this thread, it is now the third result: google.com/search?q=u+prefix+on+python
– OscarRyz
Mar 17 '10 at 19:02
1
@S.Lott: You're right. And now I know where that language reference is :)
– OscarRyz
Mar 17 '10 at 19:29
12
@OscarRyz is right. I made a google search for this 3 years later... and it got me to the answer with a sonic BOOM!
– James Bedford
Mar 29 '13 at 11:18
17
@JamesBedford, agreed. a search for "python u" came up with this result, which was just what I needed. The Python docs are great for the details, but locating something within them is a true, royal pain. A search for "u" on the python docs page returns a whole host of useless material. People who like to spout "read the docs" are ridiculously annoying, since searching the docs for something is so damn inefficient in the first place. If they weren't so bad at providing relevant results, that advice might be good.
– Josh Brown
Mar 19 '14 at 18:33
2
@S.Lott Google is my first resort. It found this page, which has the answer. Stackoverflow is far more reliable and understandable than any docs.
– denson
Jan 5 '17 at 15:40
add a comment |
34
@S.Lott, Well is not always easy to spot the reference in the exact page and line, specially when you're new to a certain language. I find easier and faster to ask here. ( See: meta.stackexchange.com/questions/16353/… ) Btw, It's amazing how quick does google indexed this thread, it is now the third result: google.com/search?q=u+prefix+on+python
– OscarRyz
Mar 17 '10 at 19:02
1
@S.Lott: You're right. And now I know where that language reference is :)
– OscarRyz
Mar 17 '10 at 19:29
12
@OscarRyz is right. I made a google search for this 3 years later... and it got me to the answer with a sonic BOOM!
– James Bedford
Mar 29 '13 at 11:18
17
@JamesBedford, agreed. a search for "python u" came up with this result, which was just what I needed. The Python docs are great for the details, but locating something within them is a true, royal pain. A search for "u" on the python docs page returns a whole host of useless material. People who like to spout "read the docs" are ridiculously annoying, since searching the docs for something is so damn inefficient in the first place. If they weren't so bad at providing relevant results, that advice might be good.
– Josh Brown
Mar 19 '14 at 18:33
2
@S.Lott Google is my first resort. It found this page, which has the answer. Stackoverflow is far more reliable and understandable than any docs.
– denson
Jan 5 '17 at 15:40
34
34
@S.Lott, Well is not always easy to spot the reference in the exact page and line, specially when you're new to a certain language. I find easier and faster to ask here. ( See: meta.stackexchange.com/questions/16353/… ) Btw, It's amazing how quick does google indexed this thread, it is now the third result: google.com/search?q=u+prefix+on+python
– OscarRyz
Mar 17 '10 at 19:02
@S.Lott, Well is not always easy to spot the reference in the exact page and line, specially when you're new to a certain language. I find easier and faster to ask here. ( See: meta.stackexchange.com/questions/16353/… ) Btw, It's amazing how quick does google indexed this thread, it is now the third result: google.com/search?q=u+prefix+on+python
– OscarRyz
Mar 17 '10 at 19:02
1
1
@S.Lott: You're right. And now I know where that language reference is :)
– OscarRyz
Mar 17 '10 at 19:29
@S.Lott: You're right. And now I know where that language reference is :)
– OscarRyz
Mar 17 '10 at 19:29
12
12
@OscarRyz is right. I made a google search for this 3 years later... and it got me to the answer with a sonic BOOM!
– James Bedford
Mar 29 '13 at 11:18
@OscarRyz is right. I made a google search for this 3 years later... and it got me to the answer with a sonic BOOM!
– James Bedford
Mar 29 '13 at 11:18
17
17
@JamesBedford, agreed. a search for "python u" came up with this result, which was just what I needed. The Python docs are great for the details, but locating something within them is a true, royal pain. A search for "u" on the python docs page returns a whole host of useless material. People who like to spout "read the docs" are ridiculously annoying, since searching the docs for something is so damn inefficient in the first place. If they weren't so bad at providing relevant results, that advice might be good.
– Josh Brown
Mar 19 '14 at 18:33
@JamesBedford, agreed. a search for "python u" came up with this result, which was just what I needed. The Python docs are great for the details, but locating something within them is a true, royal pain. A search for "u" on the python docs page returns a whole host of useless material. People who like to spout "read the docs" are ridiculously annoying, since searching the docs for something is so damn inefficient in the first place. If they weren't so bad at providing relevant results, that advice might be good.
– Josh Brown
Mar 19 '14 at 18:33
2
2
@S.Lott Google is my first resort. It found this page, which has the answer. Stackoverflow is far more reliable and understandable than any docs.
– denson
Jan 5 '17 at 15:40
@S.Lott Google is my first resort. It found this page, which has the answer. Stackoverflow is far more reliable and understandable than any docs.
– denson
Jan 5 '17 at 15:40
add a comment |
7 Answers
7
active
oldest
votes
You're right, see 3.1.3. Unicode Strings.
It's been the syntax since Python 2.0.
1
One should add that it is not necessary in Python 3 anymore, but still valid syntax.
– Martin Thoma
Nov 21 '18 at 8:31
add a comment |
The u in u'Some String'
means that your string is a Unicode string.
Q: I'm in a terrible, awful hurry and I landed here from Google Search. I'm trying to write this data to a file, I'm getting an error, and I need the dead simplest, probably flawed, solution this second.
A: You should really read Joel's Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) essay on character sets.
Q: sry no time code pls
A: Fine. try str('Some String')
or 'Some String'.encode('ascii', 'ignore')
. But you should really read some of the answers and discussion on Converting a Unicode string and this excellent, excellent, primer on character encoding.
6
This works if the string contains ASCII text only. In all other cases you'll have to explicitly encode.
– Martijn Pieters♦
Sep 13 '14 at 13:55
2
This treats the u'' as something "to get rid of". This tells me that you don't actually understand what it is. You generally do not just want to "get rid of" it, and the correct way to make a byte string from a Unicode string depends on what that string contains and in which context.
– Lennart Regebro
Dec 9 '14 at 12:32
1
@LennartRegebro totally agreed - this was a throwaway answer that was meant to be tongue in cheek, but it accumulated a sort of horrifying number of upvotes. edited to try to steer folks in the right direction.
– Andrew
Feb 5 '15 at 22:28
add a comment |
My guess is that it indicates "Unicode", is it correct?
Yes.
If so, since when is it available?
Python 2.x.
In Python 3.x the strings use Unicode by default and there's no need for the u
prefix. Note: in Python 3.0-3.2, the u is a syntax error. In Python 3.3+ it's legal again to make it easier to write 2/3 compatible apps.
7
+1 For the 3.x note thank you
– OscarRyz
Mar 17 '10 at 18:47
4
It's even a Syntax Error in Python 3 to use theu
prefix.
– Tim Pietzcker
Mar 17 '10 at 18:53
12
@TimPietzcker: Only in 3.0-3.2; in 3.3+ it's legal (and meaningless), to make it easier to write 2.6+/3.3+ single-codebase libraries and apps.
– abarnert
Sep 11 '14 at 23:59
@abarnert: Well, that comment is now four-and-a-half years old :)
– Tim Pietzcker
Sep 12 '14 at 7:59
2
@TimPietzcker: Sure, but just as your comment was a useful addendum for anyone finding this useful answer by search in 2010, I think it's useful to mention the change in 3.3 to anyone finding it in 2014. It might arguably be better to edit the answer, but I think it's a minor point that most people won't run into (because unless you're still using 3.0-3.2 in 2014, "no need for the prefix" is all you need to know).
– abarnert
Sep 12 '14 at 17:36
|
show 1 more comment
The following should help:
http://docs.python.org/library/functions.html#unicode
http://www.amk.ca/python/howto/unicode (skip down to "Python's Unicode Support" if you're already familiar with Unicode principles)
add a comment |
All strings meant for humans should use u"".
I found that the following mindset helps a lot when dealing with Python strings: All Python manifest strings should use the u""
syntax. The ""
syntax is for byte arrays, only.
Before the bashing begins, let me explain. Most Python programs start out with using ""
for strings. But then they need to support documentation off the Internet, so they start using "".decode
and all of a sudden they are getting exceptions everywhere about decoding this and that - all because of the use of ""
for strings. In this case, Unicode does act like a virus and will wreak havoc.
But, if you follow my rule, you won't have this infection (because you will already be infected).
bash -c "echo Shouldn\'t you use b\"...\" for byte arrays?"
– kennytm
Mar 17 '10 at 19:06
@KennyTM Sounds good! Simply meant to say all strings meant for humans should useu""
.
– Frank Krueger
Mar 17 '10 at 19:38
If you want to religiously use Unicode everywhere—which, for many applications (but not all), is a good thing—you almost certainly want Python 3.x, not 2.x. That may not have been true in 2010 when this was written, but in 2014, most libraries or platforms that prevent you from upgrading to 3.x will also prevent you from using Unicode properly…
– abarnert
Sep 12 '14 at 0:01
add a comment |
It's Unicode.
Just put the variable between str()
, and it will work fine.
But in case you have two lists like the following:
a = ['co32','co36']
b = [u'co32',u'co36']
If you check set(a)==set(b)
, it will come as False, but if you do as follows:
b = str(b)
set(a)==set(b)
Now, the result will be True.
Danger, danger. You should never encode a Unicode (str()
oru'€'.encode()
) without passing an encoding. If the string contains non-ASCII the user will receive a UnicodeEncodeException.
– Alastair McCormack
Dec 26 '16 at 17:21
1
Further, your code doesn't work.b = str(b)
just gives the stringrepr()
of the list, i.e.b = "[u'co32', u'co36']"
. Thenset(a)==set(b) = False
– Alastair McCormack
Dec 26 '16 at 17:23
add a comment |
I came here because I had funny-char-syndrome on my requests
output. I thought response.text
would give me a properly decoded string, but in the output I found funny double-chars where German umlauts should have been.
Turns out response.encoding
was empty somehow and so the response did not know how to properly decode the content and just treated it as ASCII (I guess).
My solution was to get the raw bytes with 'response.content' and manually apply decode('utf_8')
to it. The result was schöne Umlaute.
The correctly decoded
für
vs. the improperly decoded
fĂźr
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2464959%2fwhats-the-u-prefix-in-a-python-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're right, see 3.1.3. Unicode Strings.
It's been the syntax since Python 2.0.
1
One should add that it is not necessary in Python 3 anymore, but still valid syntax.
– Martin Thoma
Nov 21 '18 at 8:31
add a comment |
You're right, see 3.1.3. Unicode Strings.
It's been the syntax since Python 2.0.
1
One should add that it is not necessary in Python 3 anymore, but still valid syntax.
– Martin Thoma
Nov 21 '18 at 8:31
add a comment |
You're right, see 3.1.3. Unicode Strings.
It's been the syntax since Python 2.0.
You're right, see 3.1.3. Unicode Strings.
It's been the syntax since Python 2.0.
edited Oct 9 '17 at 18:52
Peter Mortensen
13.7k1986113
13.7k1986113
answered Mar 17 '10 at 18:45
Stefan KendallStefan Kendall
41.5k56212370
41.5k56212370
1
One should add that it is not necessary in Python 3 anymore, but still valid syntax.
– Martin Thoma
Nov 21 '18 at 8:31
add a comment |
1
One should add that it is not necessary in Python 3 anymore, but still valid syntax.
– Martin Thoma
Nov 21 '18 at 8:31
1
1
One should add that it is not necessary in Python 3 anymore, but still valid syntax.
– Martin Thoma
Nov 21 '18 at 8:31
One should add that it is not necessary in Python 3 anymore, but still valid syntax.
– Martin Thoma
Nov 21 '18 at 8:31
add a comment |
The u in u'Some String'
means that your string is a Unicode string.
Q: I'm in a terrible, awful hurry and I landed here from Google Search. I'm trying to write this data to a file, I'm getting an error, and I need the dead simplest, probably flawed, solution this second.
A: You should really read Joel's Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) essay on character sets.
Q: sry no time code pls
A: Fine. try str('Some String')
or 'Some String'.encode('ascii', 'ignore')
. But you should really read some of the answers and discussion on Converting a Unicode string and this excellent, excellent, primer on character encoding.
6
This works if the string contains ASCII text only. In all other cases you'll have to explicitly encode.
– Martijn Pieters♦
Sep 13 '14 at 13:55
2
This treats the u'' as something "to get rid of". This tells me that you don't actually understand what it is. You generally do not just want to "get rid of" it, and the correct way to make a byte string from a Unicode string depends on what that string contains and in which context.
– Lennart Regebro
Dec 9 '14 at 12:32
1
@LennartRegebro totally agreed - this was a throwaway answer that was meant to be tongue in cheek, but it accumulated a sort of horrifying number of upvotes. edited to try to steer folks in the right direction.
– Andrew
Feb 5 '15 at 22:28
add a comment |
The u in u'Some String'
means that your string is a Unicode string.
Q: I'm in a terrible, awful hurry and I landed here from Google Search. I'm trying to write this data to a file, I'm getting an error, and I need the dead simplest, probably flawed, solution this second.
A: You should really read Joel's Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) essay on character sets.
Q: sry no time code pls
A: Fine. try str('Some String')
or 'Some String'.encode('ascii', 'ignore')
. But you should really read some of the answers and discussion on Converting a Unicode string and this excellent, excellent, primer on character encoding.
6
This works if the string contains ASCII text only. In all other cases you'll have to explicitly encode.
– Martijn Pieters♦
Sep 13 '14 at 13:55
2
This treats the u'' as something "to get rid of". This tells me that you don't actually understand what it is. You generally do not just want to "get rid of" it, and the correct way to make a byte string from a Unicode string depends on what that string contains and in which context.
– Lennart Regebro
Dec 9 '14 at 12:32
1
@LennartRegebro totally agreed - this was a throwaway answer that was meant to be tongue in cheek, but it accumulated a sort of horrifying number of upvotes. edited to try to steer folks in the right direction.
– Andrew
Feb 5 '15 at 22:28
add a comment |
The u in u'Some String'
means that your string is a Unicode string.
Q: I'm in a terrible, awful hurry and I landed here from Google Search. I'm trying to write this data to a file, I'm getting an error, and I need the dead simplest, probably flawed, solution this second.
A: You should really read Joel's Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) essay on character sets.
Q: sry no time code pls
A: Fine. try str('Some String')
or 'Some String'.encode('ascii', 'ignore')
. But you should really read some of the answers and discussion on Converting a Unicode string and this excellent, excellent, primer on character encoding.
The u in u'Some String'
means that your string is a Unicode string.
Q: I'm in a terrible, awful hurry and I landed here from Google Search. I'm trying to write this data to a file, I'm getting an error, and I need the dead simplest, probably flawed, solution this second.
A: You should really read Joel's Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) essay on character sets.
Q: sry no time code pls
A: Fine. try str('Some String')
or 'Some String'.encode('ascii', 'ignore')
. But you should really read some of the answers and discussion on Converting a Unicode string and this excellent, excellent, primer on character encoding.
edited Oct 9 '17 at 18:56
Peter Mortensen
13.7k1986113
13.7k1986113
answered May 24 '13 at 18:55
AndrewAndrew
6,80883453
6,80883453
6
This works if the string contains ASCII text only. In all other cases you'll have to explicitly encode.
– Martijn Pieters♦
Sep 13 '14 at 13:55
2
This treats the u'' as something "to get rid of". This tells me that you don't actually understand what it is. You generally do not just want to "get rid of" it, and the correct way to make a byte string from a Unicode string depends on what that string contains and in which context.
– Lennart Regebro
Dec 9 '14 at 12:32
1
@LennartRegebro totally agreed - this was a throwaway answer that was meant to be tongue in cheek, but it accumulated a sort of horrifying number of upvotes. edited to try to steer folks in the right direction.
– Andrew
Feb 5 '15 at 22:28
add a comment |
6
This works if the string contains ASCII text only. In all other cases you'll have to explicitly encode.
– Martijn Pieters♦
Sep 13 '14 at 13:55
2
This treats the u'' as something "to get rid of". This tells me that you don't actually understand what it is. You generally do not just want to "get rid of" it, and the correct way to make a byte string from a Unicode string depends on what that string contains and in which context.
– Lennart Regebro
Dec 9 '14 at 12:32
1
@LennartRegebro totally agreed - this was a throwaway answer that was meant to be tongue in cheek, but it accumulated a sort of horrifying number of upvotes. edited to try to steer folks in the right direction.
– Andrew
Feb 5 '15 at 22:28
6
6
This works if the string contains ASCII text only. In all other cases you'll have to explicitly encode.
– Martijn Pieters♦
Sep 13 '14 at 13:55
This works if the string contains ASCII text only. In all other cases you'll have to explicitly encode.
– Martijn Pieters♦
Sep 13 '14 at 13:55
2
2
This treats the u'' as something "to get rid of". This tells me that you don't actually understand what it is. You generally do not just want to "get rid of" it, and the correct way to make a byte string from a Unicode string depends on what that string contains and in which context.
– Lennart Regebro
Dec 9 '14 at 12:32
This treats the u'' as something "to get rid of". This tells me that you don't actually understand what it is. You generally do not just want to "get rid of" it, and the correct way to make a byte string from a Unicode string depends on what that string contains and in which context.
– Lennart Regebro
Dec 9 '14 at 12:32
1
1
@LennartRegebro totally agreed - this was a throwaway answer that was meant to be tongue in cheek, but it accumulated a sort of horrifying number of upvotes. edited to try to steer folks in the right direction.
– Andrew
Feb 5 '15 at 22:28
@LennartRegebro totally agreed - this was a throwaway answer that was meant to be tongue in cheek, but it accumulated a sort of horrifying number of upvotes. edited to try to steer folks in the right direction.
– Andrew
Feb 5 '15 at 22:28
add a comment |
My guess is that it indicates "Unicode", is it correct?
Yes.
If so, since when is it available?
Python 2.x.
In Python 3.x the strings use Unicode by default and there's no need for the u
prefix. Note: in Python 3.0-3.2, the u is a syntax error. In Python 3.3+ it's legal again to make it easier to write 2/3 compatible apps.
7
+1 For the 3.x note thank you
– OscarRyz
Mar 17 '10 at 18:47
4
It's even a Syntax Error in Python 3 to use theu
prefix.
– Tim Pietzcker
Mar 17 '10 at 18:53
12
@TimPietzcker: Only in 3.0-3.2; in 3.3+ it's legal (and meaningless), to make it easier to write 2.6+/3.3+ single-codebase libraries and apps.
– abarnert
Sep 11 '14 at 23:59
@abarnert: Well, that comment is now four-and-a-half years old :)
– Tim Pietzcker
Sep 12 '14 at 7:59
2
@TimPietzcker: Sure, but just as your comment was a useful addendum for anyone finding this useful answer by search in 2010, I think it's useful to mention the change in 3.3 to anyone finding it in 2014. It might arguably be better to edit the answer, but I think it's a minor point that most people won't run into (because unless you're still using 3.0-3.2 in 2014, "no need for the prefix" is all you need to know).
– abarnert
Sep 12 '14 at 17:36
|
show 1 more comment
My guess is that it indicates "Unicode", is it correct?
Yes.
If so, since when is it available?
Python 2.x.
In Python 3.x the strings use Unicode by default and there's no need for the u
prefix. Note: in Python 3.0-3.2, the u is a syntax error. In Python 3.3+ it's legal again to make it easier to write 2/3 compatible apps.
7
+1 For the 3.x note thank you
– OscarRyz
Mar 17 '10 at 18:47
4
It's even a Syntax Error in Python 3 to use theu
prefix.
– Tim Pietzcker
Mar 17 '10 at 18:53
12
@TimPietzcker: Only in 3.0-3.2; in 3.3+ it's legal (and meaningless), to make it easier to write 2.6+/3.3+ single-codebase libraries and apps.
– abarnert
Sep 11 '14 at 23:59
@abarnert: Well, that comment is now four-and-a-half years old :)
– Tim Pietzcker
Sep 12 '14 at 7:59
2
@TimPietzcker: Sure, but just as your comment was a useful addendum for anyone finding this useful answer by search in 2010, I think it's useful to mention the change in 3.3 to anyone finding it in 2014. It might arguably be better to edit the answer, but I think it's a minor point that most people won't run into (because unless you're still using 3.0-3.2 in 2014, "no need for the prefix" is all you need to know).
– abarnert
Sep 12 '14 at 17:36
|
show 1 more comment
My guess is that it indicates "Unicode", is it correct?
Yes.
If so, since when is it available?
Python 2.x.
In Python 3.x the strings use Unicode by default and there's no need for the u
prefix. Note: in Python 3.0-3.2, the u is a syntax error. In Python 3.3+ it's legal again to make it easier to write 2/3 compatible apps.
My guess is that it indicates "Unicode", is it correct?
Yes.
If so, since when is it available?
Python 2.x.
In Python 3.x the strings use Unicode by default and there's no need for the u
prefix. Note: in Python 3.0-3.2, the u is a syntax error. In Python 3.3+ it's legal again to make it easier to write 2/3 compatible apps.
edited Mar 26 '18 at 17:39
Jacinda
3,42222035
3,42222035
answered Mar 17 '10 at 18:45
kennytmkennytm
406k80914920
406k80914920
7
+1 For the 3.x note thank you
– OscarRyz
Mar 17 '10 at 18:47
4
It's even a Syntax Error in Python 3 to use theu
prefix.
– Tim Pietzcker
Mar 17 '10 at 18:53
12
@TimPietzcker: Only in 3.0-3.2; in 3.3+ it's legal (and meaningless), to make it easier to write 2.6+/3.3+ single-codebase libraries and apps.
– abarnert
Sep 11 '14 at 23:59
@abarnert: Well, that comment is now four-and-a-half years old :)
– Tim Pietzcker
Sep 12 '14 at 7:59
2
@TimPietzcker: Sure, but just as your comment was a useful addendum for anyone finding this useful answer by search in 2010, I think it's useful to mention the change in 3.3 to anyone finding it in 2014. It might arguably be better to edit the answer, but I think it's a minor point that most people won't run into (because unless you're still using 3.0-3.2 in 2014, "no need for the prefix" is all you need to know).
– abarnert
Sep 12 '14 at 17:36
|
show 1 more comment
7
+1 For the 3.x note thank you
– OscarRyz
Mar 17 '10 at 18:47
4
It's even a Syntax Error in Python 3 to use theu
prefix.
– Tim Pietzcker
Mar 17 '10 at 18:53
12
@TimPietzcker: Only in 3.0-3.2; in 3.3+ it's legal (and meaningless), to make it easier to write 2.6+/3.3+ single-codebase libraries and apps.
– abarnert
Sep 11 '14 at 23:59
@abarnert: Well, that comment is now four-and-a-half years old :)
– Tim Pietzcker
Sep 12 '14 at 7:59
2
@TimPietzcker: Sure, but just as your comment was a useful addendum for anyone finding this useful answer by search in 2010, I think it's useful to mention the change in 3.3 to anyone finding it in 2014. It might arguably be better to edit the answer, but I think it's a minor point that most people won't run into (because unless you're still using 3.0-3.2 in 2014, "no need for the prefix" is all you need to know).
– abarnert
Sep 12 '14 at 17:36
7
7
+1 For the 3.x note thank you
– OscarRyz
Mar 17 '10 at 18:47
+1 For the 3.x note thank you
– OscarRyz
Mar 17 '10 at 18:47
4
4
It's even a Syntax Error in Python 3 to use the
u
prefix.– Tim Pietzcker
Mar 17 '10 at 18:53
It's even a Syntax Error in Python 3 to use the
u
prefix.– Tim Pietzcker
Mar 17 '10 at 18:53
12
12
@TimPietzcker: Only in 3.0-3.2; in 3.3+ it's legal (and meaningless), to make it easier to write 2.6+/3.3+ single-codebase libraries and apps.
– abarnert
Sep 11 '14 at 23:59
@TimPietzcker: Only in 3.0-3.2; in 3.3+ it's legal (and meaningless), to make it easier to write 2.6+/3.3+ single-codebase libraries and apps.
– abarnert
Sep 11 '14 at 23:59
@abarnert: Well, that comment is now four-and-a-half years old :)
– Tim Pietzcker
Sep 12 '14 at 7:59
@abarnert: Well, that comment is now four-and-a-half years old :)
– Tim Pietzcker
Sep 12 '14 at 7:59
2
2
@TimPietzcker: Sure, but just as your comment was a useful addendum for anyone finding this useful answer by search in 2010, I think it's useful to mention the change in 3.3 to anyone finding it in 2014. It might arguably be better to edit the answer, but I think it's a minor point that most people won't run into (because unless you're still using 3.0-3.2 in 2014, "no need for the prefix" is all you need to know).
– abarnert
Sep 12 '14 at 17:36
@TimPietzcker: Sure, but just as your comment was a useful addendum for anyone finding this useful answer by search in 2010, I think it's useful to mention the change in 3.3 to anyone finding it in 2014. It might arguably be better to edit the answer, but I think it's a minor point that most people won't run into (because unless you're still using 3.0-3.2 in 2014, "no need for the prefix" is all you need to know).
– abarnert
Sep 12 '14 at 17:36
|
show 1 more comment
The following should help:
http://docs.python.org/library/functions.html#unicode
http://www.amk.ca/python/howto/unicode (skip down to "Python's Unicode Support" if you're already familiar with Unicode principles)
add a comment |
The following should help:
http://docs.python.org/library/functions.html#unicode
http://www.amk.ca/python/howto/unicode (skip down to "Python's Unicode Support" if you're already familiar with Unicode principles)
add a comment |
The following should help:
http://docs.python.org/library/functions.html#unicode
http://www.amk.ca/python/howto/unicode (skip down to "Python's Unicode Support" if you're already familiar with Unicode principles)
The following should help:
http://docs.python.org/library/functions.html#unicode
http://www.amk.ca/python/howto/unicode (skip down to "Python's Unicode Support" if you're already familiar with Unicode principles)
edited Mar 8 '12 at 13:31
Bill the Lizard
295k157499792
295k157499792
answered Mar 17 '10 at 18:50
Brian LuftBrian Luft
1,065712
1,065712
add a comment |
add a comment |
All strings meant for humans should use u"".
I found that the following mindset helps a lot when dealing with Python strings: All Python manifest strings should use the u""
syntax. The ""
syntax is for byte arrays, only.
Before the bashing begins, let me explain. Most Python programs start out with using ""
for strings. But then they need to support documentation off the Internet, so they start using "".decode
and all of a sudden they are getting exceptions everywhere about decoding this and that - all because of the use of ""
for strings. In this case, Unicode does act like a virus and will wreak havoc.
But, if you follow my rule, you won't have this infection (because you will already be infected).
bash -c "echo Shouldn\'t you use b\"...\" for byte arrays?"
– kennytm
Mar 17 '10 at 19:06
@KennyTM Sounds good! Simply meant to say all strings meant for humans should useu""
.
– Frank Krueger
Mar 17 '10 at 19:38
If you want to religiously use Unicode everywhere—which, for many applications (but not all), is a good thing—you almost certainly want Python 3.x, not 2.x. That may not have been true in 2010 when this was written, but in 2014, most libraries or platforms that prevent you from upgrading to 3.x will also prevent you from using Unicode properly…
– abarnert
Sep 12 '14 at 0:01
add a comment |
All strings meant for humans should use u"".
I found that the following mindset helps a lot when dealing with Python strings: All Python manifest strings should use the u""
syntax. The ""
syntax is for byte arrays, only.
Before the bashing begins, let me explain. Most Python programs start out with using ""
for strings. But then they need to support documentation off the Internet, so they start using "".decode
and all of a sudden they are getting exceptions everywhere about decoding this and that - all because of the use of ""
for strings. In this case, Unicode does act like a virus and will wreak havoc.
But, if you follow my rule, you won't have this infection (because you will already be infected).
bash -c "echo Shouldn\'t you use b\"...\" for byte arrays?"
– kennytm
Mar 17 '10 at 19:06
@KennyTM Sounds good! Simply meant to say all strings meant for humans should useu""
.
– Frank Krueger
Mar 17 '10 at 19:38
If you want to religiously use Unicode everywhere—which, for many applications (but not all), is a good thing—you almost certainly want Python 3.x, not 2.x. That may not have been true in 2010 when this was written, but in 2014, most libraries or platforms that prevent you from upgrading to 3.x will also prevent you from using Unicode properly…
– abarnert
Sep 12 '14 at 0:01
add a comment |
All strings meant for humans should use u"".
I found that the following mindset helps a lot when dealing with Python strings: All Python manifest strings should use the u""
syntax. The ""
syntax is for byte arrays, only.
Before the bashing begins, let me explain. Most Python programs start out with using ""
for strings. But then they need to support documentation off the Internet, so they start using "".decode
and all of a sudden they are getting exceptions everywhere about decoding this and that - all because of the use of ""
for strings. In this case, Unicode does act like a virus and will wreak havoc.
But, if you follow my rule, you won't have this infection (because you will already be infected).
All strings meant for humans should use u"".
I found that the following mindset helps a lot when dealing with Python strings: All Python manifest strings should use the u""
syntax. The ""
syntax is for byte arrays, only.
Before the bashing begins, let me explain. Most Python programs start out with using ""
for strings. But then they need to support documentation off the Internet, so they start using "".decode
and all of a sudden they are getting exceptions everywhere about decoding this and that - all because of the use of ""
for strings. In this case, Unicode does act like a virus and will wreak havoc.
But, if you follow my rule, you won't have this infection (because you will already be infected).
edited Oct 9 '17 at 18:54
Peter Mortensen
13.7k1986113
13.7k1986113
answered Mar 17 '10 at 18:51
Frank KruegerFrank Krueger
44.9k41143198
44.9k41143198
bash -c "echo Shouldn\'t you use b\"...\" for byte arrays?"
– kennytm
Mar 17 '10 at 19:06
@KennyTM Sounds good! Simply meant to say all strings meant for humans should useu""
.
– Frank Krueger
Mar 17 '10 at 19:38
If you want to religiously use Unicode everywhere—which, for many applications (but not all), is a good thing—you almost certainly want Python 3.x, not 2.x. That may not have been true in 2010 when this was written, but in 2014, most libraries or platforms that prevent you from upgrading to 3.x will also prevent you from using Unicode properly…
– abarnert
Sep 12 '14 at 0:01
add a comment |
bash -c "echo Shouldn\'t you use b\"...\" for byte arrays?"
– kennytm
Mar 17 '10 at 19:06
@KennyTM Sounds good! Simply meant to say all strings meant for humans should useu""
.
– Frank Krueger
Mar 17 '10 at 19:38
If you want to religiously use Unicode everywhere—which, for many applications (but not all), is a good thing—you almost certainly want Python 3.x, not 2.x. That may not have been true in 2010 when this was written, but in 2014, most libraries or platforms that prevent you from upgrading to 3.x will also prevent you from using Unicode properly…
– abarnert
Sep 12 '14 at 0:01
bash -c "echo Shouldn\'t you use b\"...\" for byte arrays?"
– kennytm
Mar 17 '10 at 19:06
bash -c "echo Shouldn\'t you use b\"...\" for byte arrays?"
– kennytm
Mar 17 '10 at 19:06
@KennyTM Sounds good! Simply meant to say all strings meant for humans should use
u""
.– Frank Krueger
Mar 17 '10 at 19:38
@KennyTM Sounds good! Simply meant to say all strings meant for humans should use
u""
.– Frank Krueger
Mar 17 '10 at 19:38
If you want to religiously use Unicode everywhere—which, for many applications (but not all), is a good thing—you almost certainly want Python 3.x, not 2.x. That may not have been true in 2010 when this was written, but in 2014, most libraries or platforms that prevent you from upgrading to 3.x will also prevent you from using Unicode properly…
– abarnert
Sep 12 '14 at 0:01
If you want to religiously use Unicode everywhere—which, for many applications (but not all), is a good thing—you almost certainly want Python 3.x, not 2.x. That may not have been true in 2010 when this was written, but in 2014, most libraries or platforms that prevent you from upgrading to 3.x will also prevent you from using Unicode properly…
– abarnert
Sep 12 '14 at 0:01
add a comment |
It's Unicode.
Just put the variable between str()
, and it will work fine.
But in case you have two lists like the following:
a = ['co32','co36']
b = [u'co32',u'co36']
If you check set(a)==set(b)
, it will come as False, but if you do as follows:
b = str(b)
set(a)==set(b)
Now, the result will be True.
Danger, danger. You should never encode a Unicode (str()
oru'€'.encode()
) without passing an encoding. If the string contains non-ASCII the user will receive a UnicodeEncodeException.
– Alastair McCormack
Dec 26 '16 at 17:21
1
Further, your code doesn't work.b = str(b)
just gives the stringrepr()
of the list, i.e.b = "[u'co32', u'co36']"
. Thenset(a)==set(b) = False
– Alastair McCormack
Dec 26 '16 at 17:23
add a comment |
It's Unicode.
Just put the variable between str()
, and it will work fine.
But in case you have two lists like the following:
a = ['co32','co36']
b = [u'co32',u'co36']
If you check set(a)==set(b)
, it will come as False, but if you do as follows:
b = str(b)
set(a)==set(b)
Now, the result will be True.
Danger, danger. You should never encode a Unicode (str()
oru'€'.encode()
) without passing an encoding. If the string contains non-ASCII the user will receive a UnicodeEncodeException.
– Alastair McCormack
Dec 26 '16 at 17:21
1
Further, your code doesn't work.b = str(b)
just gives the stringrepr()
of the list, i.e.b = "[u'co32', u'co36']"
. Thenset(a)==set(b) = False
– Alastair McCormack
Dec 26 '16 at 17:23
add a comment |
It's Unicode.
Just put the variable between str()
, and it will work fine.
But in case you have two lists like the following:
a = ['co32','co36']
b = [u'co32',u'co36']
If you check set(a)==set(b)
, it will come as False, but if you do as follows:
b = str(b)
set(a)==set(b)
Now, the result will be True.
It's Unicode.
Just put the variable between str()
, and it will work fine.
But in case you have two lists like the following:
a = ['co32','co36']
b = [u'co32',u'co36']
If you check set(a)==set(b)
, it will come as False, but if you do as follows:
b = str(b)
set(a)==set(b)
Now, the result will be True.
edited Oct 9 '17 at 18:58
Peter Mortensen
13.7k1986113
13.7k1986113
answered Dec 9 '14 at 12:28
user3251882user3251882
348414
348414
Danger, danger. You should never encode a Unicode (str()
oru'€'.encode()
) without passing an encoding. If the string contains non-ASCII the user will receive a UnicodeEncodeException.
– Alastair McCormack
Dec 26 '16 at 17:21
1
Further, your code doesn't work.b = str(b)
just gives the stringrepr()
of the list, i.e.b = "[u'co32', u'co36']"
. Thenset(a)==set(b) = False
– Alastair McCormack
Dec 26 '16 at 17:23
add a comment |
Danger, danger. You should never encode a Unicode (str()
oru'€'.encode()
) without passing an encoding. If the string contains non-ASCII the user will receive a UnicodeEncodeException.
– Alastair McCormack
Dec 26 '16 at 17:21
1
Further, your code doesn't work.b = str(b)
just gives the stringrepr()
of the list, i.e.b = "[u'co32', u'co36']"
. Thenset(a)==set(b) = False
– Alastair McCormack
Dec 26 '16 at 17:23
Danger, danger. You should never encode a Unicode (
str()
or u'€'.encode()
) without passing an encoding. If the string contains non-ASCII the user will receive a UnicodeEncodeException.– Alastair McCormack
Dec 26 '16 at 17:21
Danger, danger. You should never encode a Unicode (
str()
or u'€'.encode()
) without passing an encoding. If the string contains non-ASCII the user will receive a UnicodeEncodeException.– Alastair McCormack
Dec 26 '16 at 17:21
1
1
Further, your code doesn't work.
b = str(b)
just gives the string repr()
of the list, i.e. b = "[u'co32', u'co36']"
. Then set(a)==set(b) = False
– Alastair McCormack
Dec 26 '16 at 17:23
Further, your code doesn't work.
b = str(b)
just gives the string repr()
of the list, i.e. b = "[u'co32', u'co36']"
. Then set(a)==set(b) = False
– Alastair McCormack
Dec 26 '16 at 17:23
add a comment |
I came here because I had funny-char-syndrome on my requests
output. I thought response.text
would give me a properly decoded string, but in the output I found funny double-chars where German umlauts should have been.
Turns out response.encoding
was empty somehow and so the response did not know how to properly decode the content and just treated it as ASCII (I guess).
My solution was to get the raw bytes with 'response.content' and manually apply decode('utf_8')
to it. The result was schöne Umlaute.
The correctly decoded
für
vs. the improperly decoded
fĂźr
add a comment |
I came here because I had funny-char-syndrome on my requests
output. I thought response.text
would give me a properly decoded string, but in the output I found funny double-chars where German umlauts should have been.
Turns out response.encoding
was empty somehow and so the response did not know how to properly decode the content and just treated it as ASCII (I guess).
My solution was to get the raw bytes with 'response.content' and manually apply decode('utf_8')
to it. The result was schöne Umlaute.
The correctly decoded
für
vs. the improperly decoded
fĂźr
add a comment |
I came here because I had funny-char-syndrome on my requests
output. I thought response.text
would give me a properly decoded string, but in the output I found funny double-chars where German umlauts should have been.
Turns out response.encoding
was empty somehow and so the response did not know how to properly decode the content and just treated it as ASCII (I guess).
My solution was to get the raw bytes with 'response.content' and manually apply decode('utf_8')
to it. The result was schöne Umlaute.
The correctly decoded
für
vs. the improperly decoded
fĂźr
I came here because I had funny-char-syndrome on my requests
output. I thought response.text
would give me a properly decoded string, but in the output I found funny double-chars where German umlauts should have been.
Turns out response.encoding
was empty somehow and so the response did not know how to properly decode the content and just treated it as ASCII (I guess).
My solution was to get the raw bytes with 'response.content' and manually apply decode('utf_8')
to it. The result was schöne Umlaute.
The correctly decoded
für
vs. the improperly decoded
fĂźr
answered Nov 14 '17 at 16:14
ChrisChris
1,80311323
1,80311323
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2464959%2fwhats-the-u-prefix-in-a-python-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
34
@S.Lott, Well is not always easy to spot the reference in the exact page and line, specially when you're new to a certain language. I find easier and faster to ask here. ( See: meta.stackexchange.com/questions/16353/… ) Btw, It's amazing how quick does google indexed this thread, it is now the third result: google.com/search?q=u+prefix+on+python
– OscarRyz
Mar 17 '10 at 19:02
1
@S.Lott: You're right. And now I know where that language reference is :)
– OscarRyz
Mar 17 '10 at 19:29
12
@OscarRyz is right. I made a google search for this 3 years later... and it got me to the answer with a sonic BOOM!
– James Bedford
Mar 29 '13 at 11:18
17
@JamesBedford, agreed. a search for "python u" came up with this result, which was just what I needed. The Python docs are great for the details, but locating something within them is a true, royal pain. A search for "u" on the python docs page returns a whole host of useless material. People who like to spout "read the docs" are ridiculously annoying, since searching the docs for something is so damn inefficient in the first place. If they weren't so bad at providing relevant results, that advice might be good.
– Josh Brown
Mar 19 '14 at 18:33
2
@S.Lott Google is my first resort. It found this page, which has the answer. Stackoverflow is far more reliable and understandable than any docs.
– denson
Jan 5 '17 at 15:40