Obtain 2nd Character in a list [closed]
I am trying to create a loop function whereby if the 2nd character of each word in a sentence = 'R', then it will be printed out.
st = 'Print only the words'
for word in st.split():
if word[1] == 'r':
print(word)
I keep getting the error string index is out of range.
python
closed as off-topic by tripleee, jww, andrew_reece, Machavity, Makyen Jan 4 at 22:27
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – tripleee, jww, andrew_reece, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am trying to create a loop function whereby if the 2nd character of each word in a sentence = 'R', then it will be printed out.
st = 'Print only the words'
for word in st.split():
if word[1] == 'r':
print(word)
I keep getting the error string index is out of range.
python
closed as off-topic by tripleee, jww, andrew_reece, Machavity, Makyen Jan 4 at 22:27
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – tripleee, jww, andrew_reece, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.
5
Works for me. You sure this is the exact example? No input data with 1-letter words like'a'
?
– schwobaseggl
Jan 2 at 4:50
It works for me too.
– GeekSambhu
Jan 2 at 4:51
Either catch the exception or check the string length.
– t.m.adam
Jan 2 at 4:53
@schwobaseggl nope this is just a simplified version of what I'm trying to achieve. I think I made a mistake in the qn somewhere! Thanks!
– Javier
Jan 2 at 4:53
in question statement you mentionsR
and in code you are usingr
. is your 2nd character is case sensitive ?
– prashant rana
Jan 2 at 5:39
add a comment |
I am trying to create a loop function whereby if the 2nd character of each word in a sentence = 'R', then it will be printed out.
st = 'Print only the words'
for word in st.split():
if word[1] == 'r':
print(word)
I keep getting the error string index is out of range.
python
I am trying to create a loop function whereby if the 2nd character of each word in a sentence = 'R', then it will be printed out.
st = 'Print only the words'
for word in st.split():
if word[1] == 'r':
print(word)
I keep getting the error string index is out of range.
python
python
asked Jan 2 at 4:48
JavierJavier
314212
314212
closed as off-topic by tripleee, jww, andrew_reece, Machavity, Makyen Jan 4 at 22:27
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – tripleee, jww, andrew_reece, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by tripleee, jww, andrew_reece, Machavity, Makyen Jan 4 at 22:27
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – tripleee, jww, andrew_reece, Machavity
If this question can be reworded to fit the rules in the help center, please edit the question.
5
Works for me. You sure this is the exact example? No input data with 1-letter words like'a'
?
– schwobaseggl
Jan 2 at 4:50
It works for me too.
– GeekSambhu
Jan 2 at 4:51
Either catch the exception or check the string length.
– t.m.adam
Jan 2 at 4:53
@schwobaseggl nope this is just a simplified version of what I'm trying to achieve. I think I made a mistake in the qn somewhere! Thanks!
– Javier
Jan 2 at 4:53
in question statement you mentionsR
and in code you are usingr
. is your 2nd character is case sensitive ?
– prashant rana
Jan 2 at 5:39
add a comment |
5
Works for me. You sure this is the exact example? No input data with 1-letter words like'a'
?
– schwobaseggl
Jan 2 at 4:50
It works for me too.
– GeekSambhu
Jan 2 at 4:51
Either catch the exception or check the string length.
– t.m.adam
Jan 2 at 4:53
@schwobaseggl nope this is just a simplified version of what I'm trying to achieve. I think I made a mistake in the qn somewhere! Thanks!
– Javier
Jan 2 at 4:53
in question statement you mentionsR
and in code you are usingr
. is your 2nd character is case sensitive ?
– prashant rana
Jan 2 at 5:39
5
5
Works for me. You sure this is the exact example? No input data with 1-letter words like
'a'
?– schwobaseggl
Jan 2 at 4:50
Works for me. You sure this is the exact example? No input data with 1-letter words like
'a'
?– schwobaseggl
Jan 2 at 4:50
It works for me too.
– GeekSambhu
Jan 2 at 4:51
It works for me too.
– GeekSambhu
Jan 2 at 4:51
Either catch the exception or check the string length.
– t.m.adam
Jan 2 at 4:53
Either catch the exception or check the string length.
– t.m.adam
Jan 2 at 4:53
@schwobaseggl nope this is just a simplified version of what I'm trying to achieve. I think I made a mistake in the qn somewhere! Thanks!
– Javier
Jan 2 at 4:53
@schwobaseggl nope this is just a simplified version of what I'm trying to achieve. I think I made a mistake in the qn somewhere! Thanks!
– Javier
Jan 2 at 4:53
in question statement you mentions
R
and in code you are using r
. is your 2nd character is case sensitive ?– prashant rana
Jan 2 at 5:39
in question statement you mentions
R
and in code you are using r
. is your 2nd character is case sensitive ?– prashant rana
Jan 2 at 5:39
add a comment |
3 Answers
3
active
oldest
votes
You can explicitly filter out words with len(word) < 2
, that should solve your IndexError
problem:
st = 'Print only a word with at least two letters that has r in index 2'
for word in st.split():
if len(word) > 1 and word[1] == 'r':
print(word)
# 'Print'
add a comment |
The index is out of range if the word only has one letter.
st = 'Print only a word, orange'
for word in st.split():
if len(word) > 1:
if word[1] == 'r':
print(word)
My answer and andrew_reece's answer are identical in operation. He answered four minutes before me and therefore should get the credit. I am leaving my answer open, since splitting the if may be easier to read for new users to python.
– Strom
Jan 2 at 8:17
add a comment |
How about using regexp ?
import re
re.findall(r'b(.r.*?)b',"Print the string or trim the string your lucky dry day")
['Print', 'or', 'trim', 'dry']
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can explicitly filter out words with len(word) < 2
, that should solve your IndexError
problem:
st = 'Print only a word with at least two letters that has r in index 2'
for word in st.split():
if len(word) > 1 and word[1] == 'r':
print(word)
# 'Print'
add a comment |
You can explicitly filter out words with len(word) < 2
, that should solve your IndexError
problem:
st = 'Print only a word with at least two letters that has r in index 2'
for word in st.split():
if len(word) > 1 and word[1] == 'r':
print(word)
# 'Print'
add a comment |
You can explicitly filter out words with len(word) < 2
, that should solve your IndexError
problem:
st = 'Print only a word with at least two letters that has r in index 2'
for word in st.split():
if len(word) > 1 and word[1] == 'r':
print(word)
# 'Print'
You can explicitly filter out words with len(word) < 2
, that should solve your IndexError
problem:
st = 'Print only a word with at least two letters that has r in index 2'
for word in st.split():
if len(word) > 1 and word[1] == 'r':
print(word)
# 'Print'
answered Jan 2 at 4:55
andrew_reeceandrew_reece
10.6k2928
10.6k2928
add a comment |
add a comment |
The index is out of range if the word only has one letter.
st = 'Print only a word, orange'
for word in st.split():
if len(word) > 1:
if word[1] == 'r':
print(word)
My answer and andrew_reece's answer are identical in operation. He answered four minutes before me and therefore should get the credit. I am leaving my answer open, since splitting the if may be easier to read for new users to python.
– Strom
Jan 2 at 8:17
add a comment |
The index is out of range if the word only has one letter.
st = 'Print only a word, orange'
for word in st.split():
if len(word) > 1:
if word[1] == 'r':
print(word)
My answer and andrew_reece's answer are identical in operation. He answered four minutes before me and therefore should get the credit. I am leaving my answer open, since splitting the if may be easier to read for new users to python.
– Strom
Jan 2 at 8:17
add a comment |
The index is out of range if the word only has one letter.
st = 'Print only a word, orange'
for word in st.split():
if len(word) > 1:
if word[1] == 'r':
print(word)
The index is out of range if the word only has one letter.
st = 'Print only a word, orange'
for word in st.split():
if len(word) > 1:
if word[1] == 'r':
print(word)
answered Jan 2 at 4:58
StromStrom
2,303322
2,303322
My answer and andrew_reece's answer are identical in operation. He answered four minutes before me and therefore should get the credit. I am leaving my answer open, since splitting the if may be easier to read for new users to python.
– Strom
Jan 2 at 8:17
add a comment |
My answer and andrew_reece's answer are identical in operation. He answered four minutes before me and therefore should get the credit. I am leaving my answer open, since splitting the if may be easier to read for new users to python.
– Strom
Jan 2 at 8:17
My answer and andrew_reece's answer are identical in operation. He answered four minutes before me and therefore should get the credit. I am leaving my answer open, since splitting the if may be easier to read for new users to python.
– Strom
Jan 2 at 8:17
My answer and andrew_reece's answer are identical in operation. He answered four minutes before me and therefore should get the credit. I am leaving my answer open, since splitting the if may be easier to read for new users to python.
– Strom
Jan 2 at 8:17
add a comment |
How about using regexp ?
import re
re.findall(r'b(.r.*?)b',"Print the string or trim the string your lucky dry day")
['Print', 'or', 'trim', 'dry']
add a comment |
How about using regexp ?
import re
re.findall(r'b(.r.*?)b',"Print the string or trim the string your lucky dry day")
['Print', 'or', 'trim', 'dry']
add a comment |
How about using regexp ?
import re
re.findall(r'b(.r.*?)b',"Print the string or trim the string your lucky dry day")
['Print', 'or', 'trim', 'dry']
How about using regexp ?
import re
re.findall(r'b(.r.*?)b',"Print the string or trim the string your lucky dry day")
['Print', 'or', 'trim', 'dry']
answered Jan 2 at 5:40
sakhunzaisakhunzai
6,6281167115
6,6281167115
add a comment |
add a comment |
5
Works for me. You sure this is the exact example? No input data with 1-letter words like
'a'
?– schwobaseggl
Jan 2 at 4:50
It works for me too.
– GeekSambhu
Jan 2 at 4:51
Either catch the exception or check the string length.
– t.m.adam
Jan 2 at 4:53
@schwobaseggl nope this is just a simplified version of what I'm trying to achieve. I think I made a mistake in the qn somewhere! Thanks!
– Javier
Jan 2 at 4:53
in question statement you mentions
R
and in code you are usingr
. is your 2nd character is case sensitive ?– prashant rana
Jan 2 at 5:39