How to find a word in a sentence that starts and ends with the same letter?
Is there a way to write "for" loop and "If Else" in python that searches through a sentence and find the number of words that start and end with the same letter?
I've tried writing something like:
sentence = "Mom knock the door"
list = sentence.split()
for word in sentence:
if ...
python
add a comment |
Is there a way to write "for" loop and "If Else" in python that searches through a sentence and find the number of words that start and end with the same letter?
I've tried writing something like:
sentence = "Mom knock the door"
list = sentence.split()
for word in sentence:
if ...
python
add a comment |
Is there a way to write "for" loop and "If Else" in python that searches through a sentence and find the number of words that start and end with the same letter?
I've tried writing something like:
sentence = "Mom knock the door"
list = sentence.split()
for word in sentence:
if ...
python
Is there a way to write "for" loop and "If Else" in python that searches through a sentence and find the number of words that start and end with the same letter?
I've tried writing something like:
sentence = "Mom knock the door"
list = sentence.split()
for word in sentence:
if ...
python
python
edited Dec 29 '18 at 9:23
Ha Bom
8872418
8872418
asked Dec 29 '18 at 8:44
FarzanFarzan
83
83
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
simply compare the character at the beginning of the string with the character at the end of the string like so:
if word[0] == word[-1]:
If it shouldn't be case sensitive, lower the word first by calling:
word = word.lower()
It's cleaner to .lower() the original sentence if case-sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:53
1
@Jason That is true. However, I just wanted to be sure that the for-loop works for him, as he already split the string at the beginning.
– DWuest
Dec 29 '18 at 8:55
add a comment |
words_list = sentence.split()
new_words_list =
for word in words_list:
if word[0] == word[-1]:
new_words_list.append(word)
print('Number of words that starts and ends with same letter - {}'.format(len(new_words_list)))
Also you can do it with list comprehension:
new_words_list = [word for word in words_list if word[0] == word[-1]]
If you want not to have it case sensitive use word[0].lower()
and word[-1].lower()
instead of word[0]
and word[-1]
This solution is case-sensitive. The author didn't specify the same case in the question -- only that it is the same letter. I would use .lower() on the original sentence if case sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:49
The author didn't add if it's required to be case sensitive or not, so I added that option in my answer.
– Sergey Pugach
Dec 29 '18 at 8:54
add a comment |
The answers above are all smart, I prefer to deal with it in functional programming way, like this:
sentence = "Mom knock the door"
def is_same_letter_at_begin_end(word):
return word and word[0].lower() == word[-1].lower()
target_words = list(filter(is_same_letter_at_begin_end, sentence.split()))
print(target_words)
print(len(target_words))
You can just doreturn word and word[0].lower() == word[-1].lower()
, no need for the conditional. Nice use offilter
.
– snakecharmerb
Dec 30 '18 at 8:56
@snakecharmerb thx, I change it.
– Menglong Li
Dec 30 '18 at 10:37
add a comment |
list = sentence.split(" ")
count = 0
for word in list:
lc_word = word.lower()
if lc_word[0] == lc_word[-1]:
count +=1
add a comment |
lst = sentence.split()
num_words = 0
for i in lst:
low = i.lower()
if low[0] == low[len(low)-1]:
num_words += 1
return num_words
1
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Robert Columbia
Dec 29 '18 at 11:12
add a comment |
list
or set
comprehension case insensitive:
sentence = "Mom knock the door, mom"
all_words_count = len([ word for word in sentence.split() if word[0].lower() == word[-1].lower() ])
uniq_words_count = len({word.lower() for word in sentence.split() if word[0].lower() == word[-1].lower()})
print(all_words_count) #=> 3
print(uniq_words_count) #=> 2
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%2f53968057%2fhow-to-find-a-word-in-a-sentence-that-starts-and-ends-with-the-same-letter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
simply compare the character at the beginning of the string with the character at the end of the string like so:
if word[0] == word[-1]:
If it shouldn't be case sensitive, lower the word first by calling:
word = word.lower()
It's cleaner to .lower() the original sentence if case-sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:53
1
@Jason That is true. However, I just wanted to be sure that the for-loop works for him, as he already split the string at the beginning.
– DWuest
Dec 29 '18 at 8:55
add a comment |
simply compare the character at the beginning of the string with the character at the end of the string like so:
if word[0] == word[-1]:
If it shouldn't be case sensitive, lower the word first by calling:
word = word.lower()
It's cleaner to .lower() the original sentence if case-sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:53
1
@Jason That is true. However, I just wanted to be sure that the for-loop works for him, as he already split the string at the beginning.
– DWuest
Dec 29 '18 at 8:55
add a comment |
simply compare the character at the beginning of the string with the character at the end of the string like so:
if word[0] == word[-1]:
If it shouldn't be case sensitive, lower the word first by calling:
word = word.lower()
simply compare the character at the beginning of the string with the character at the end of the string like so:
if word[0] == word[-1]:
If it shouldn't be case sensitive, lower the word first by calling:
word = word.lower()
answered Dec 29 '18 at 8:48
DWuestDWuest
45712
45712
It's cleaner to .lower() the original sentence if case-sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:53
1
@Jason That is true. However, I just wanted to be sure that the for-loop works for him, as he already split the string at the beginning.
– DWuest
Dec 29 '18 at 8:55
add a comment |
It's cleaner to .lower() the original sentence if case-sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:53
1
@Jason That is true. However, I just wanted to be sure that the for-loop works for him, as he already split the string at the beginning.
– DWuest
Dec 29 '18 at 8:55
It's cleaner to .lower() the original sentence if case-sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:53
It's cleaner to .lower() the original sentence if case-sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:53
1
1
@Jason That is true. However, I just wanted to be sure that the for-loop works for him, as he already split the string at the beginning.
– DWuest
Dec 29 '18 at 8:55
@Jason That is true. However, I just wanted to be sure that the for-loop works for him, as he already split the string at the beginning.
– DWuest
Dec 29 '18 at 8:55
add a comment |
words_list = sentence.split()
new_words_list =
for word in words_list:
if word[0] == word[-1]:
new_words_list.append(word)
print('Number of words that starts and ends with same letter - {}'.format(len(new_words_list)))
Also you can do it with list comprehension:
new_words_list = [word for word in words_list if word[0] == word[-1]]
If you want not to have it case sensitive use word[0].lower()
and word[-1].lower()
instead of word[0]
and word[-1]
This solution is case-sensitive. The author didn't specify the same case in the question -- only that it is the same letter. I would use .lower() on the original sentence if case sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:49
The author didn't add if it's required to be case sensitive or not, so I added that option in my answer.
– Sergey Pugach
Dec 29 '18 at 8:54
add a comment |
words_list = sentence.split()
new_words_list =
for word in words_list:
if word[0] == word[-1]:
new_words_list.append(word)
print('Number of words that starts and ends with same letter - {}'.format(len(new_words_list)))
Also you can do it with list comprehension:
new_words_list = [word for word in words_list if word[0] == word[-1]]
If you want not to have it case sensitive use word[0].lower()
and word[-1].lower()
instead of word[0]
and word[-1]
This solution is case-sensitive. The author didn't specify the same case in the question -- only that it is the same letter. I would use .lower() on the original sentence if case sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:49
The author didn't add if it's required to be case sensitive or not, so I added that option in my answer.
– Sergey Pugach
Dec 29 '18 at 8:54
add a comment |
words_list = sentence.split()
new_words_list =
for word in words_list:
if word[0] == word[-1]:
new_words_list.append(word)
print('Number of words that starts and ends with same letter - {}'.format(len(new_words_list)))
Also you can do it with list comprehension:
new_words_list = [word for word in words_list if word[0] == word[-1]]
If you want not to have it case sensitive use word[0].lower()
and word[-1].lower()
instead of word[0]
and word[-1]
words_list = sentence.split()
new_words_list =
for word in words_list:
if word[0] == word[-1]:
new_words_list.append(word)
print('Number of words that starts and ends with same letter - {}'.format(len(new_words_list)))
Also you can do it with list comprehension:
new_words_list = [word for word in words_list if word[0] == word[-1]]
If you want not to have it case sensitive use word[0].lower()
and word[-1].lower()
instead of word[0]
and word[-1]
edited Dec 29 '18 at 8:52
answered Dec 29 '18 at 8:48
Sergey PugachSergey Pugach
1,8021412
1,8021412
This solution is case-sensitive. The author didn't specify the same case in the question -- only that it is the same letter. I would use .lower() on the original sentence if case sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:49
The author didn't add if it's required to be case sensitive or not, so I added that option in my answer.
– Sergey Pugach
Dec 29 '18 at 8:54
add a comment |
This solution is case-sensitive. The author didn't specify the same case in the question -- only that it is the same letter. I would use .lower() on the original sentence if case sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:49
The author didn't add if it's required to be case sensitive or not, so I added that option in my answer.
– Sergey Pugach
Dec 29 '18 at 8:54
This solution is case-sensitive. The author didn't specify the same case in the question -- only that it is the same letter. I would use .lower() on the original sentence if case sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:49
This solution is case-sensitive. The author didn't specify the same case in the question -- only that it is the same letter. I would use .lower() on the original sentence if case sensitivity isn't a requirement.
– Jason Baumgartner
Dec 29 '18 at 8:49
The author didn't add if it's required to be case sensitive or not, so I added that option in my answer.
– Sergey Pugach
Dec 29 '18 at 8:54
The author didn't add if it's required to be case sensitive or not, so I added that option in my answer.
– Sergey Pugach
Dec 29 '18 at 8:54
add a comment |
The answers above are all smart, I prefer to deal with it in functional programming way, like this:
sentence = "Mom knock the door"
def is_same_letter_at_begin_end(word):
return word and word[0].lower() == word[-1].lower()
target_words = list(filter(is_same_letter_at_begin_end, sentence.split()))
print(target_words)
print(len(target_words))
You can just doreturn word and word[0].lower() == word[-1].lower()
, no need for the conditional. Nice use offilter
.
– snakecharmerb
Dec 30 '18 at 8:56
@snakecharmerb thx, I change it.
– Menglong Li
Dec 30 '18 at 10:37
add a comment |
The answers above are all smart, I prefer to deal with it in functional programming way, like this:
sentence = "Mom knock the door"
def is_same_letter_at_begin_end(word):
return word and word[0].lower() == word[-1].lower()
target_words = list(filter(is_same_letter_at_begin_end, sentence.split()))
print(target_words)
print(len(target_words))
You can just doreturn word and word[0].lower() == word[-1].lower()
, no need for the conditional. Nice use offilter
.
– snakecharmerb
Dec 30 '18 at 8:56
@snakecharmerb thx, I change it.
– Menglong Li
Dec 30 '18 at 10:37
add a comment |
The answers above are all smart, I prefer to deal with it in functional programming way, like this:
sentence = "Mom knock the door"
def is_same_letter_at_begin_end(word):
return word and word[0].lower() == word[-1].lower()
target_words = list(filter(is_same_letter_at_begin_end, sentence.split()))
print(target_words)
print(len(target_words))
The answers above are all smart, I prefer to deal with it in functional programming way, like this:
sentence = "Mom knock the door"
def is_same_letter_at_begin_end(word):
return word and word[0].lower() == word[-1].lower()
target_words = list(filter(is_same_letter_at_begin_end, sentence.split()))
print(target_words)
print(len(target_words))
edited Dec 30 '18 at 10:37
answered Dec 29 '18 at 9:06
Menglong LiMenglong Li
1,250414
1,250414
You can just doreturn word and word[0].lower() == word[-1].lower()
, no need for the conditional. Nice use offilter
.
– snakecharmerb
Dec 30 '18 at 8:56
@snakecharmerb thx, I change it.
– Menglong Li
Dec 30 '18 at 10:37
add a comment |
You can just doreturn word and word[0].lower() == word[-1].lower()
, no need for the conditional. Nice use offilter
.
– snakecharmerb
Dec 30 '18 at 8:56
@snakecharmerb thx, I change it.
– Menglong Li
Dec 30 '18 at 10:37
You can just do
return word and word[0].lower() == word[-1].lower()
, no need for the conditional. Nice use of filter
.– snakecharmerb
Dec 30 '18 at 8:56
You can just do
return word and word[0].lower() == word[-1].lower()
, no need for the conditional. Nice use of filter
.– snakecharmerb
Dec 30 '18 at 8:56
@snakecharmerb thx, I change it.
– Menglong Li
Dec 30 '18 at 10:37
@snakecharmerb thx, I change it.
– Menglong Li
Dec 30 '18 at 10:37
add a comment |
list = sentence.split(" ")
count = 0
for word in list:
lc_word = word.lower()
if lc_word[0] == lc_word[-1]:
count +=1
add a comment |
list = sentence.split(" ")
count = 0
for word in list:
lc_word = word.lower()
if lc_word[0] == lc_word[-1]:
count +=1
add a comment |
list = sentence.split(" ")
count = 0
for word in list:
lc_word = word.lower()
if lc_word[0] == lc_word[-1]:
count +=1
list = sentence.split(" ")
count = 0
for word in list:
lc_word = word.lower()
if lc_word[0] == lc_word[-1]:
count +=1
edited Dec 29 '18 at 8:57
answered Dec 29 '18 at 8:48
Will WardWill Ward
5714
5714
add a comment |
add a comment |
lst = sentence.split()
num_words = 0
for i in lst:
low = i.lower()
if low[0] == low[len(low)-1]:
num_words += 1
return num_words
1
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Robert Columbia
Dec 29 '18 at 11:12
add a comment |
lst = sentence.split()
num_words = 0
for i in lst:
low = i.lower()
if low[0] == low[len(low)-1]:
num_words += 1
return num_words
1
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Robert Columbia
Dec 29 '18 at 11:12
add a comment |
lst = sentence.split()
num_words = 0
for i in lst:
low = i.lower()
if low[0] == low[len(low)-1]:
num_words += 1
return num_words
lst = sentence.split()
num_words = 0
for i in lst:
low = i.lower()
if low[0] == low[len(low)-1]:
num_words += 1
return num_words
answered Dec 29 '18 at 9:00
Aditya GoyalAditya Goyal
11
11
1
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Robert Columbia
Dec 29 '18 at 11:12
add a comment |
1
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Robert Columbia
Dec 29 '18 at 11:12
1
1
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Robert Columbia
Dec 29 '18 at 11:12
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Robert Columbia
Dec 29 '18 at 11:12
add a comment |
list
or set
comprehension case insensitive:
sentence = "Mom knock the door, mom"
all_words_count = len([ word for word in sentence.split() if word[0].lower() == word[-1].lower() ])
uniq_words_count = len({word.lower() for word in sentence.split() if word[0].lower() == word[-1].lower()})
print(all_words_count) #=> 3
print(uniq_words_count) #=> 2
add a comment |
list
or set
comprehension case insensitive:
sentence = "Mom knock the door, mom"
all_words_count = len([ word for word in sentence.split() if word[0].lower() == word[-1].lower() ])
uniq_words_count = len({word.lower() for word in sentence.split() if word[0].lower() == word[-1].lower()})
print(all_words_count) #=> 3
print(uniq_words_count) #=> 2
add a comment |
list
or set
comprehension case insensitive:
sentence = "Mom knock the door, mom"
all_words_count = len([ word for word in sentence.split() if word[0].lower() == word[-1].lower() ])
uniq_words_count = len({word.lower() for word in sentence.split() if word[0].lower() == word[-1].lower()})
print(all_words_count) #=> 3
print(uniq_words_count) #=> 2
list
or set
comprehension case insensitive:
sentence = "Mom knock the door, mom"
all_words_count = len([ word for word in sentence.split() if word[0].lower() == word[-1].lower() ])
uniq_words_count = len({word.lower() for word in sentence.split() if word[0].lower() == word[-1].lower()})
print(all_words_count) #=> 3
print(uniq_words_count) #=> 2
answered Dec 29 '18 at 9:22
iGianiGian
3,7502622
3,7502622
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%2f53968057%2fhow-to-find-a-word-in-a-sentence-that-starts-and-ends-with-the-same-letter%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