Python adding “u” to string

Multi tool use
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a string "E001"
I would like to add "u" to the beginning of that string
So the code would look something like this:
output = add_u("E001")
print(output)
--------------------------
OUTPUT: uE001
There are difficulties with this I keep getting a Unicode error
python unicode
add a comment |
I have a string "E001"
I would like to add "u" to the beginning of that string
So the code would look something like this:
output = add_u("E001")
print(output)
--------------------------
OUTPUT: uE001
There are difficulties with this I keep getting a Unicode error
python unicode
1
Ok, and what happened when you tried the approaches from your research?
– roganjosh
Jan 3 at 22:09
1
What's your ultimate goal?
– Felix Kling
Jan 3 at 22:09
It's a simple string concatenation; the only tricky part is that you need to escape the backslash.
– chepner
Jan 3 at 22:10
The things that kept happening in my research where: it kept adding more then one or gave a Unicode error
– Quinten Cabo
Jan 3 at 22:20
My ultimate goal is that there is a json file that remaps unicode characters to images so it would be like {"char":"uE001","file":"path/to/image"}
– Quinten Cabo
Jan 3 at 22:22
add a comment |
I have a string "E001"
I would like to add "u" to the beginning of that string
So the code would look something like this:
output = add_u("E001")
print(output)
--------------------------
OUTPUT: uE001
There are difficulties with this I keep getting a Unicode error
python unicode
I have a string "E001"
I would like to add "u" to the beginning of that string
So the code would look something like this:
output = add_u("E001")
print(output)
--------------------------
OUTPUT: uE001
There are difficulties with this I keep getting a Unicode error
python unicode
python unicode
edited Jan 3 at 22:10
Quinten Cabo
asked Jan 3 at 22:07


Quinten CaboQuinten Cabo
45
45
1
Ok, and what happened when you tried the approaches from your research?
– roganjosh
Jan 3 at 22:09
1
What's your ultimate goal?
– Felix Kling
Jan 3 at 22:09
It's a simple string concatenation; the only tricky part is that you need to escape the backslash.
– chepner
Jan 3 at 22:10
The things that kept happening in my research where: it kept adding more then one or gave a Unicode error
– Quinten Cabo
Jan 3 at 22:20
My ultimate goal is that there is a json file that remaps unicode characters to images so it would be like {"char":"uE001","file":"path/to/image"}
– Quinten Cabo
Jan 3 at 22:22
add a comment |
1
Ok, and what happened when you tried the approaches from your research?
– roganjosh
Jan 3 at 22:09
1
What's your ultimate goal?
– Felix Kling
Jan 3 at 22:09
It's a simple string concatenation; the only tricky part is that you need to escape the backslash.
– chepner
Jan 3 at 22:10
The things that kept happening in my research where: it kept adding more then one or gave a Unicode error
– Quinten Cabo
Jan 3 at 22:20
My ultimate goal is that there is a json file that remaps unicode characters to images so it would be like {"char":"uE001","file":"path/to/image"}
– Quinten Cabo
Jan 3 at 22:22
1
1
Ok, and what happened when you tried the approaches from your research?
– roganjosh
Jan 3 at 22:09
Ok, and what happened when you tried the approaches from your research?
– roganjosh
Jan 3 at 22:09
1
1
What's your ultimate goal?
– Felix Kling
Jan 3 at 22:09
What's your ultimate goal?
– Felix Kling
Jan 3 at 22:09
It's a simple string concatenation; the only tricky part is that you need to escape the backslash.
– chepner
Jan 3 at 22:10
It's a simple string concatenation; the only tricky part is that you need to escape the backslash.
– chepner
Jan 3 at 22:10
The things that kept happening in my research where: it kept adding more then one or gave a Unicode error
– Quinten Cabo
Jan 3 at 22:20
The things that kept happening in my research where: it kept adding more then one or gave a Unicode error
– Quinten Cabo
Jan 3 at 22:20
My ultimate goal is that there is a json file that remaps unicode characters to images so it would be like {"char":"uE001","file":"path/to/image"}
– Quinten Cabo
Jan 3 at 22:22
My ultimate goal is that there is a json file that remaps unicode characters to images so it would be like {"char":"uE001","file":"path/to/image"}
– Quinten Cabo
Jan 3 at 22:22
add a comment |
2 Answers
2
active
oldest
votes
Try this:
def add_u(s):
return r'u' + s
print(add_u('E001'))
Outputs:
uE001
This worked! What exactly does the r in front of the string mean? I have come across when I tried to solve this on my own but I find the meaning unclear.
– Quinten Cabo
Jan 3 at 22:24
The r means raw string literal. stackoverflow.com/questions/2081640/…
– Luke DeLuccia
Jan 3 at 22:31
So when I add this string to a list it adds a second "", a_list = ["uE001"] becomes ["\uE001"] is there any way to prevent this? I have the string in a variable if you do this literally nothing will happen but if you return a variable from a function and put that in a list then this happens
– Quinten Cabo
Jan 3 at 23:06
You're looking at the representation of the string, not at the string content. If you havex = '` in your code then
x` will be a string with one character, the backslash. See the difference withprint(x, repr(x))
.
– Matthias
Jan 4 at 8:02
add a comment |
How about just:
output = "\u" + "E001"
print(output)
Or as a function:
def add_u(string):
return "\u" + string
output = add_u("E001")
print(output)
Handles backslash escaping.
This won't work I really need there to be one ""
– Quinten Cabo
Jan 3 at 22:19
1
@QuintenCabo There will only be one backslash in the output.
– mypetlion
Jan 3 at 22:21
I was testing this in a console and there it did 2 but running it from a script it indeed had 1 ""
– Quinten Cabo
Jan 3 at 22:28
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%2f54030455%2fpython-adding-u-to-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
def add_u(s):
return r'u' + s
print(add_u('E001'))
Outputs:
uE001
This worked! What exactly does the r in front of the string mean? I have come across when I tried to solve this on my own but I find the meaning unclear.
– Quinten Cabo
Jan 3 at 22:24
The r means raw string literal. stackoverflow.com/questions/2081640/…
– Luke DeLuccia
Jan 3 at 22:31
So when I add this string to a list it adds a second "", a_list = ["uE001"] becomes ["\uE001"] is there any way to prevent this? I have the string in a variable if you do this literally nothing will happen but if you return a variable from a function and put that in a list then this happens
– Quinten Cabo
Jan 3 at 23:06
You're looking at the representation of the string, not at the string content. If you havex = '` in your code then
x` will be a string with one character, the backslash. See the difference withprint(x, repr(x))
.
– Matthias
Jan 4 at 8:02
add a comment |
Try this:
def add_u(s):
return r'u' + s
print(add_u('E001'))
Outputs:
uE001
This worked! What exactly does the r in front of the string mean? I have come across when I tried to solve this on my own but I find the meaning unclear.
– Quinten Cabo
Jan 3 at 22:24
The r means raw string literal. stackoverflow.com/questions/2081640/…
– Luke DeLuccia
Jan 3 at 22:31
So when I add this string to a list it adds a second "", a_list = ["uE001"] becomes ["\uE001"] is there any way to prevent this? I have the string in a variable if you do this literally nothing will happen but if you return a variable from a function and put that in a list then this happens
– Quinten Cabo
Jan 3 at 23:06
You're looking at the representation of the string, not at the string content. If you havex = '` in your code then
x` will be a string with one character, the backslash. See the difference withprint(x, repr(x))
.
– Matthias
Jan 4 at 8:02
add a comment |
Try this:
def add_u(s):
return r'u' + s
print(add_u('E001'))
Outputs:
uE001
Try this:
def add_u(s):
return r'u' + s
print(add_u('E001'))
Outputs:
uE001
answered Jan 3 at 22:11


Luke DeLucciaLuke DeLuccia
456212
456212
This worked! What exactly does the r in front of the string mean? I have come across when I tried to solve this on my own but I find the meaning unclear.
– Quinten Cabo
Jan 3 at 22:24
The r means raw string literal. stackoverflow.com/questions/2081640/…
– Luke DeLuccia
Jan 3 at 22:31
So when I add this string to a list it adds a second "", a_list = ["uE001"] becomes ["\uE001"] is there any way to prevent this? I have the string in a variable if you do this literally nothing will happen but if you return a variable from a function and put that in a list then this happens
– Quinten Cabo
Jan 3 at 23:06
You're looking at the representation of the string, not at the string content. If you havex = '` in your code then
x` will be a string with one character, the backslash. See the difference withprint(x, repr(x))
.
– Matthias
Jan 4 at 8:02
add a comment |
This worked! What exactly does the r in front of the string mean? I have come across when I tried to solve this on my own but I find the meaning unclear.
– Quinten Cabo
Jan 3 at 22:24
The r means raw string literal. stackoverflow.com/questions/2081640/…
– Luke DeLuccia
Jan 3 at 22:31
So when I add this string to a list it adds a second "", a_list = ["uE001"] becomes ["\uE001"] is there any way to prevent this? I have the string in a variable if you do this literally nothing will happen but if you return a variable from a function and put that in a list then this happens
– Quinten Cabo
Jan 3 at 23:06
You're looking at the representation of the string, not at the string content. If you havex = '` in your code then
x` will be a string with one character, the backslash. See the difference withprint(x, repr(x))
.
– Matthias
Jan 4 at 8:02
This worked! What exactly does the r in front of the string mean? I have come across when I tried to solve this on my own but I find the meaning unclear.
– Quinten Cabo
Jan 3 at 22:24
This worked! What exactly does the r in front of the string mean? I have come across when I tried to solve this on my own but I find the meaning unclear.
– Quinten Cabo
Jan 3 at 22:24
The r means raw string literal. stackoverflow.com/questions/2081640/…
– Luke DeLuccia
Jan 3 at 22:31
The r means raw string literal. stackoverflow.com/questions/2081640/…
– Luke DeLuccia
Jan 3 at 22:31
So when I add this string to a list it adds a second "", a_list = ["uE001"] becomes ["\uE001"] is there any way to prevent this? I have the string in a variable if you do this literally nothing will happen but if you return a variable from a function and put that in a list then this happens
– Quinten Cabo
Jan 3 at 23:06
So when I add this string to a list it adds a second "", a_list = ["uE001"] becomes ["\uE001"] is there any way to prevent this? I have the string in a variable if you do this literally nothing will happen but if you return a variable from a function and put that in a list then this happens
– Quinten Cabo
Jan 3 at 23:06
You're looking at the representation of the string, not at the string content. If you have
x = '` in your code then
x` will be a string with one character, the backslash. See the difference with print(x, repr(x))
.– Matthias
Jan 4 at 8:02
You're looking at the representation of the string, not at the string content. If you have
x = '` in your code then
x` will be a string with one character, the backslash. See the difference with print(x, repr(x))
.– Matthias
Jan 4 at 8:02
add a comment |
How about just:
output = "\u" + "E001"
print(output)
Or as a function:
def add_u(string):
return "\u" + string
output = add_u("E001")
print(output)
Handles backslash escaping.
This won't work I really need there to be one ""
– Quinten Cabo
Jan 3 at 22:19
1
@QuintenCabo There will only be one backslash in the output.
– mypetlion
Jan 3 at 22:21
I was testing this in a console and there it did 2 but running it from a script it indeed had 1 ""
– Quinten Cabo
Jan 3 at 22:28
add a comment |
How about just:
output = "\u" + "E001"
print(output)
Or as a function:
def add_u(string):
return "\u" + string
output = add_u("E001")
print(output)
Handles backslash escaping.
This won't work I really need there to be one ""
– Quinten Cabo
Jan 3 at 22:19
1
@QuintenCabo There will only be one backslash in the output.
– mypetlion
Jan 3 at 22:21
I was testing this in a console and there it did 2 but running it from a script it indeed had 1 ""
– Quinten Cabo
Jan 3 at 22:28
add a comment |
How about just:
output = "\u" + "E001"
print(output)
Or as a function:
def add_u(string):
return "\u" + string
output = add_u("E001")
print(output)
Handles backslash escaping.
How about just:
output = "\u" + "E001"
print(output)
Or as a function:
def add_u(string):
return "\u" + string
output = add_u("E001")
print(output)
Handles backslash escaping.
answered Jan 3 at 22:11


connectyourchargerconnectyourcharger
599424
599424
This won't work I really need there to be one ""
– Quinten Cabo
Jan 3 at 22:19
1
@QuintenCabo There will only be one backslash in the output.
– mypetlion
Jan 3 at 22:21
I was testing this in a console and there it did 2 but running it from a script it indeed had 1 ""
– Quinten Cabo
Jan 3 at 22:28
add a comment |
This won't work I really need there to be one ""
– Quinten Cabo
Jan 3 at 22:19
1
@QuintenCabo There will only be one backslash in the output.
– mypetlion
Jan 3 at 22:21
I was testing this in a console and there it did 2 but running it from a script it indeed had 1 ""
– Quinten Cabo
Jan 3 at 22:28
This won't work I really need there to be one ""
– Quinten Cabo
Jan 3 at 22:19
This won't work I really need there to be one ""
– Quinten Cabo
Jan 3 at 22:19
1
1
@QuintenCabo There will only be one backslash in the output.
– mypetlion
Jan 3 at 22:21
@QuintenCabo There will only be one backslash in the output.
– mypetlion
Jan 3 at 22:21
I was testing this in a console and there it did 2 but running it from a script it indeed had 1 ""
– Quinten Cabo
Jan 3 at 22:28
I was testing this in a console and there it did 2 but running it from a script it indeed had 1 ""
– Quinten Cabo
Jan 3 at 22:28
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%2f54030455%2fpython-adding-u-to-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
G3sBSXxMCEtRl8bBIsA4b7AntK3kR5PFMOFt2 sEEXVZGc8w3P
1
Ok, and what happened when you tried the approaches from your research?
– roganjosh
Jan 3 at 22:09
1
What's your ultimate goal?
– Felix Kling
Jan 3 at 22:09
It's a simple string concatenation; the only tricky part is that you need to escape the backslash.
– chepner
Jan 3 at 22:10
The things that kept happening in my research where: it kept adding more then one or gave a Unicode error
– Quinten Cabo
Jan 3 at 22:20
My ultimate goal is that there is a json file that remaps unicode characters to images so it would be like {"char":"uE001","file":"path/to/image"}
– Quinten Cabo
Jan 3 at 22:22