converting string into a tuple
My goal is to convert a string into a tuple
Currently, I have a string that is surround by parentheses with its contents separated by commas.
>>>'1, 4, 1994' (variable name birthday)
(1, 4, 1994) # desired output
I have been trying to use split() to convert the string into tuple, but
tuple(birthday.split())
('1,', '4,', '1994')
but the contents are surrounded by subsequent parentheses.
What pythonic methods can I use to make the conversion?
python string python-3.x python-2.7 tuples
add a comment |
My goal is to convert a string into a tuple
Currently, I have a string that is surround by parentheses with its contents separated by commas.
>>>'1, 4, 1994' (variable name birthday)
(1, 4, 1994) # desired output
I have been trying to use split() to convert the string into tuple, but
tuple(birthday.split())
('1,', '4,', '1994')
but the contents are surrounded by subsequent parentheses.
What pythonic methods can I use to make the conversion?
python string python-3.x python-2.7 tuples
1
I think one main thing here is that you need to split on the comma
– Osuman AAA
Nov 13 '18 at 11:42
add a comment |
My goal is to convert a string into a tuple
Currently, I have a string that is surround by parentheses with its contents separated by commas.
>>>'1, 4, 1994' (variable name birthday)
(1, 4, 1994) # desired output
I have been trying to use split() to convert the string into tuple, but
tuple(birthday.split())
('1,', '4,', '1994')
but the contents are surrounded by subsequent parentheses.
What pythonic methods can I use to make the conversion?
python string python-3.x python-2.7 tuples
My goal is to convert a string into a tuple
Currently, I have a string that is surround by parentheses with its contents separated by commas.
>>>'1, 4, 1994' (variable name birthday)
(1, 4, 1994) # desired output
I have been trying to use split() to convert the string into tuple, but
tuple(birthday.split())
('1,', '4,', '1994')
but the contents are surrounded by subsequent parentheses.
What pythonic methods can I use to make the conversion?
python string python-3.x python-2.7 tuples
python string python-3.x python-2.7 tuples
edited Nov 13 '18 at 11:43
jpp
97.1k2159109
97.1k2159109
asked Nov 13 '18 at 11:38
V AnonV Anon
2066
2066
1
I think one main thing here is that you need to split on the comma
– Osuman AAA
Nov 13 '18 at 11:42
add a comment |
1
I think one main thing here is that you need to split on the comma
– Osuman AAA
Nov 13 '18 at 11:42
1
1
I think one main thing here is that you need to split on the comma
– Osuman AAA
Nov 13 '18 at 11:42
I think one main thing here is that you need to split on the comma
– Osuman AAA
Nov 13 '18 at 11:42
add a comment |
3 Answers
3
active
oldest
votes
You can do like this,
In [35]: tuple(map(int,birthday.split(',')))
Out[35]: (1, 4, 1994)
Problem with your spit function. Use ,
to split.
add a comment |
You need to convert str
to int
and specify a sep
argument for str.split
:
res = tuple(map(int, '1, 4, 1994'.split(', '))) # (1, 4, 1994)
add a comment |
You can use ast.literal_eval
>>> import ast
>>> s = '1, 4, 1994'
>>> ast.literal_eval(s)
>>> (1,4,1994)
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%2f53280217%2fconverting-string-into-a-tuple%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do like this,
In [35]: tuple(map(int,birthday.split(',')))
Out[35]: (1, 4, 1994)
Problem with your spit function. Use ,
to split.
add a comment |
You can do like this,
In [35]: tuple(map(int,birthday.split(',')))
Out[35]: (1, 4, 1994)
Problem with your spit function. Use ,
to split.
add a comment |
You can do like this,
In [35]: tuple(map(int,birthday.split(',')))
Out[35]: (1, 4, 1994)
Problem with your spit function. Use ,
to split.
You can do like this,
In [35]: tuple(map(int,birthday.split(',')))
Out[35]: (1, 4, 1994)
Problem with your spit function. Use ,
to split.
answered Nov 13 '18 at 11:43
Rahul K PRahul K P
7,22122033
7,22122033
add a comment |
add a comment |
You need to convert str
to int
and specify a sep
argument for str.split
:
res = tuple(map(int, '1, 4, 1994'.split(', '))) # (1, 4, 1994)
add a comment |
You need to convert str
to int
and specify a sep
argument for str.split
:
res = tuple(map(int, '1, 4, 1994'.split(', '))) # (1, 4, 1994)
add a comment |
You need to convert str
to int
and specify a sep
argument for str.split
:
res = tuple(map(int, '1, 4, 1994'.split(', '))) # (1, 4, 1994)
You need to convert str
to int
and specify a sep
argument for str.split
:
res = tuple(map(int, '1, 4, 1994'.split(', '))) # (1, 4, 1994)
answered Nov 13 '18 at 11:41
jppjpp
97.1k2159109
97.1k2159109
add a comment |
add a comment |
You can use ast.literal_eval
>>> import ast
>>> s = '1, 4, 1994'
>>> ast.literal_eval(s)
>>> (1,4,1994)
add a comment |
You can use ast.literal_eval
>>> import ast
>>> s = '1, 4, 1994'
>>> ast.literal_eval(s)
>>> (1,4,1994)
add a comment |
You can use ast.literal_eval
>>> import ast
>>> s = '1, 4, 1994'
>>> ast.literal_eval(s)
>>> (1,4,1994)
You can use ast.literal_eval
>>> import ast
>>> s = '1, 4, 1994'
>>> ast.literal_eval(s)
>>> (1,4,1994)
answered Nov 13 '18 at 12:00
bro-grammerbro-grammer
3,20811332
3,20811332
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%2f53280217%2fconverting-string-into-a-tuple%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
1
I think one main thing here is that you need to split on the comma
– Osuman AAA
Nov 13 '18 at 11:42