Obtain 2nd Character in a list [closed]












-1















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.










share|improve this 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 mentions R and in code you are using r . is your 2nd character is case sensitive ?

    – prashant rana
    Jan 2 at 5:39
















-1















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.










share|improve this 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 mentions R and in code you are using r . is your 2nd character is case sensitive ?

    – prashant rana
    Jan 2 at 5:39














-1












-1








-1








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 mentions R and in code you are using r . is your 2nd character is case sensitive ?

    – prashant rana
    Jan 2 at 5:39














  • 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 using r . 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












3 Answers
3






active

oldest

votes


















2














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'





share|improve this answer































    1














    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)





    share|improve this answer
























    • 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





















    0














    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']







    share|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      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'





      share|improve this answer




























        2














        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'





        share|improve this answer


























          2












          2








          2







          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'





          share|improve this answer













          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'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 4:55









          andrew_reeceandrew_reece

          10.6k2928




          10.6k2928

























              1














              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)





              share|improve this answer
























              • 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


















              1














              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)





              share|improve this answer
























              • 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
















              1












              1








              1







              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)





              share|improve this answer













              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)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              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





















              • 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













              0














              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']







              share|improve this answer




























                0














                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']







                share|improve this answer


























                  0












                  0








                  0







                  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']







                  share|improve this answer













                  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']








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 5:40









                  sakhunzaisakhunzai

                  6,6281167115




                  6,6281167115















                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas