Why do two of the same strings not return as being the same when compared?












0















I have the following code:



file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()

for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')


Even when wordLowercase does equal gg, the string "identified" is not being printed. Why is this the case?










share|improve this question

























  • There're maybe some unprintable characters?

    – Hou Lu
    Nov 20 '18 at 2:20






  • 1





    lines[x].lower() is just lol.lower().

    – Austin
    Nov 20 '18 at 2:21











  • Please try gg = lines[x].lower().strip instead of gg = lines[x].lower() only. There might be some whitespace characters that gets included in gg. I suggest going for @Austin's remark and use lol.lower() instead of lines[x].lower().

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:25











  • I think you can do away with enumerate and just use for line in lines:. That way, you won't need to use lines[x] and just need to use line.

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:26











  • Maybe instead of gg = (lines[x].lower()) do gg = lol.lower().strip()

    – U9-Forward
    Nov 20 '18 at 2:32
















0















I have the following code:



file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()

for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')


Even when wordLowercase does equal gg, the string "identified" is not being printed. Why is this the case?










share|improve this question

























  • There're maybe some unprintable characters?

    – Hou Lu
    Nov 20 '18 at 2:20






  • 1





    lines[x].lower() is just lol.lower().

    – Austin
    Nov 20 '18 at 2:21











  • Please try gg = lines[x].lower().strip instead of gg = lines[x].lower() only. There might be some whitespace characters that gets included in gg. I suggest going for @Austin's remark and use lol.lower() instead of lines[x].lower().

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:25











  • I think you can do away with enumerate and just use for line in lines:. That way, you won't need to use lines[x] and just need to use line.

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:26











  • Maybe instead of gg = (lines[x].lower()) do gg = lol.lower().strip()

    – U9-Forward
    Nov 20 '18 at 2:32














0












0








0








I have the following code:



file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()

for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')


Even when wordLowercase does equal gg, the string "identified" is not being printed. Why is this the case?










share|improve this question
















I have the following code:



file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()

for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')


Even when wordLowercase does equal gg, the string "identified" is not being printed. Why is this the case?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 5:27









Sean Francis N. Ballais

1,26421932




1,26421932










asked Nov 20 '18 at 2:17









M. ChakM. Chak

134




134













  • There're maybe some unprintable characters?

    – Hou Lu
    Nov 20 '18 at 2:20






  • 1





    lines[x].lower() is just lol.lower().

    – Austin
    Nov 20 '18 at 2:21











  • Please try gg = lines[x].lower().strip instead of gg = lines[x].lower() only. There might be some whitespace characters that gets included in gg. I suggest going for @Austin's remark and use lol.lower() instead of lines[x].lower().

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:25











  • I think you can do away with enumerate and just use for line in lines:. That way, you won't need to use lines[x] and just need to use line.

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:26











  • Maybe instead of gg = (lines[x].lower()) do gg = lol.lower().strip()

    – U9-Forward
    Nov 20 '18 at 2:32



















  • There're maybe some unprintable characters?

    – Hou Lu
    Nov 20 '18 at 2:20






  • 1





    lines[x].lower() is just lol.lower().

    – Austin
    Nov 20 '18 at 2:21











  • Please try gg = lines[x].lower().strip instead of gg = lines[x].lower() only. There might be some whitespace characters that gets included in gg. I suggest going for @Austin's remark and use lol.lower() instead of lines[x].lower().

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:25











  • I think you can do away with enumerate and just use for line in lines:. That way, you won't need to use lines[x] and just need to use line.

    – Sean Francis N. Ballais
    Nov 20 '18 at 2:26











  • Maybe instead of gg = (lines[x].lower()) do gg = lol.lower().strip()

    – U9-Forward
    Nov 20 '18 at 2:32

















There're maybe some unprintable characters?

– Hou Lu
Nov 20 '18 at 2:20





There're maybe some unprintable characters?

– Hou Lu
Nov 20 '18 at 2:20




1




1





lines[x].lower() is just lol.lower().

– Austin
Nov 20 '18 at 2:21





lines[x].lower() is just lol.lower().

– Austin
Nov 20 '18 at 2:21













Please try gg = lines[x].lower().strip instead of gg = lines[x].lower() only. There might be some whitespace characters that gets included in gg. I suggest going for @Austin's remark and use lol.lower() instead of lines[x].lower().

– Sean Francis N. Ballais
Nov 20 '18 at 2:25





Please try gg = lines[x].lower().strip instead of gg = lines[x].lower() only. There might be some whitespace characters that gets included in gg. I suggest going for @Austin's remark and use lol.lower() instead of lines[x].lower().

– Sean Francis N. Ballais
Nov 20 '18 at 2:25













I think you can do away with enumerate and just use for line in lines:. That way, you won't need to use lines[x] and just need to use line.

– Sean Francis N. Ballais
Nov 20 '18 at 2:26





I think you can do away with enumerate and just use for line in lines:. That way, you won't need to use lines[x] and just need to use line.

– Sean Francis N. Ballais
Nov 20 '18 at 2:26













Maybe instead of gg = (lines[x].lower()) do gg = lol.lower().strip()

– U9-Forward
Nov 20 '18 at 2:32





Maybe instead of gg = (lines[x].lower()) do gg = lol.lower().strip()

– U9-Forward
Nov 20 '18 at 2:32












1 Answer
1






active

oldest

votes


















1














.readlines() includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip().



gg = lines[x].lower().strip()


Reference




  • https://www.tutorialspoint.com/python/file_readlines.htm






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%2f53385290%2fwhy-do-two-of-the-same-strings-not-return-as-being-the-same-when-compared%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    .readlines() includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip().



    gg = lines[x].lower().strip()


    Reference




    • https://www.tutorialspoint.com/python/file_readlines.htm






    share|improve this answer




























      1














      .readlines() includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip().



      gg = lines[x].lower().strip()


      Reference




      • https://www.tutorialspoint.com/python/file_readlines.htm






      share|improve this answer


























        1












        1








        1







        .readlines() includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip().



        gg = lines[x].lower().strip()


        Reference




        • https://www.tutorialspoint.com/python/file_readlines.htm






        share|improve this answer













        .readlines() includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip().



        gg = lines[x].lower().strip()


        Reference




        • https://www.tutorialspoint.com/python/file_readlines.htm







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 2:31









        Sean Francis N. BallaisSean Francis N. Ballais

        1,26421932




        1,26421932






























            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%2f53385290%2fwhy-do-two-of-the-same-strings-not-return-as-being-the-same-when-compared%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

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]