Why do two of the same strings not return as being the same when compared?
up vote
0
down vote
favorite
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
|
show 1 more comment
up vote
0
down vote
favorite
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
There're maybe some unprintable characters?
– Hou Lu
Nov 20 at 2:20
1
lines[x].lower()
is justlol.lower()
.
– Austin
Nov 20 at 2:21
Please trygg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.
– Sean Francis N. Ballais
Nov 20 at 2:25
I think you can do away withenumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.
– Sean Francis N. Ballais
Nov 20 at 2:26
Maybe instead ofgg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 at 2:32
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
python
edited Nov 20 at 5:27
Sean Francis N. Ballais
1,26421932
1,26421932
asked Nov 20 at 2:17
M. Chak
32
32
There're maybe some unprintable characters?
– Hou Lu
Nov 20 at 2:20
1
lines[x].lower()
is justlol.lower()
.
– Austin
Nov 20 at 2:21
Please trygg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.
– Sean Francis N. Ballais
Nov 20 at 2:25
I think you can do away withenumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.
– Sean Francis N. Ballais
Nov 20 at 2:26
Maybe instead ofgg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 at 2:32
|
show 1 more comment
There're maybe some unprintable characters?
– Hou Lu
Nov 20 at 2:20
1
lines[x].lower()
is justlol.lower()
.
– Austin
Nov 20 at 2:21
Please trygg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.
– Sean Francis N. Ballais
Nov 20 at 2:25
I think you can do away withenumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.
– Sean Francis N. Ballais
Nov 20 at 2:26
Maybe instead ofgg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 at 2:32
There're maybe some unprintable characters?
– Hou Lu
Nov 20 at 2:20
There're maybe some unprintable characters?
– Hou Lu
Nov 20 at 2:20
1
1
lines[x].lower()
is just lol.lower()
.– Austin
Nov 20 at 2:21
lines[x].lower()
is just lol.lower()
.– Austin
Nov 20 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 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 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 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 at 2:26
Maybe instead of
gg = (lines[x].lower())
do gg = lol.lower().strip()
– U9-Forward
Nov 20 at 2:32
Maybe instead of
gg = (lines[x].lower())
do gg = lol.lower().strip()
– U9-Forward
Nov 20 at 2:32
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
.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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
.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
add a comment |
up vote
1
down vote
accepted
.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
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
.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
.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
answered Nov 20 at 2:31
Sean Francis N. Ballais
1,26421932
1,26421932
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%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
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
There're maybe some unprintable characters?
– Hou Lu
Nov 20 at 2:20
1
lines[x].lower()
is justlol.lower()
.– Austin
Nov 20 at 2:21
Please try
gg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.– Sean Francis N. Ballais
Nov 20 at 2:25
I think you can do away with
enumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.– Sean Francis N. Ballais
Nov 20 at 2:26
Maybe instead of
gg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 at 2:32