converting string into a tuple












0















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?










share|improve this question




















  • 1





    I think one main thing here is that you need to split on the comma

    – Osuman AAA
    Nov 13 '18 at 11:42
















0















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?










share|improve this question




















  • 1





    I think one main thing here is that you need to split on the comma

    – Osuman AAA
    Nov 13 '18 at 11:42














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












3 Answers
3






active

oldest

votes


















4














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.






share|improve this answer































    2














    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)





    share|improve this answer































      2














      You can use ast.literal_eval



      >>> import ast
      >>> s = '1, 4, 1994'
      >>> ast.literal_eval(s)
      >>> (1,4,1994)





      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        4














        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.






        share|improve this answer




























          4














          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.






          share|improve this answer


























            4












            4








            4







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 11:43









            Rahul K PRahul K P

            7,22122033




            7,22122033

























                2














                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)





                share|improve this answer




























                  2














                  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)





                  share|improve this answer


























                    2












                    2








                    2







                    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)





                    share|improve this answer













                    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)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 11:41









                    jppjpp

                    97.1k2159109




                    97.1k2159109























                        2














                        You can use ast.literal_eval



                        >>> import ast
                        >>> s = '1, 4, 1994'
                        >>> ast.literal_eval(s)
                        >>> (1,4,1994)





                        share|improve this answer




























                          2














                          You can use ast.literal_eval



                          >>> import ast
                          >>> s = '1, 4, 1994'
                          >>> ast.literal_eval(s)
                          >>> (1,4,1994)





                          share|improve this answer


























                            2












                            2








                            2







                            You can use ast.literal_eval



                            >>> import ast
                            >>> s = '1, 4, 1994'
                            >>> ast.literal_eval(s)
                            >>> (1,4,1994)





                            share|improve this answer













                            You can use ast.literal_eval



                            >>> import ast
                            >>> s = '1, 4, 1994'
                            >>> ast.literal_eval(s)
                            >>> (1,4,1994)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 12:00









                            bro-grammerbro-grammer

                            3,20811332




                            3,20811332






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Florida Star v. B. J. F.

                                Danny Elfman

                                Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues