Python Forum
For loops, strings and printing words with even characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loops, strings and printing words with even characters
#1
I’ve got a string: "Secret agents are super good at staying hidden."

I’m trying to use a for loop to print out only the words with an even number of characters.

In my code below, I’ve assigned the string to a variable. I’ve split each word up into items in a new list by using the split function. Then I’ve counted the length of each item and assigned it to a new variable called, counted_string. Then I iterate through each item. I take each item and apply a modulus of two. If the remainder is 0 (meaning the word has an even number of characters) only then that particular item is printed. This is what I am trying to do in following code snippet I’ve written:

mystring = "Secret agents are super good at staying hidden."
mystring.split()
counted_string = len(mystring)
int(counted_string)
for i in counted_string:    
    if i %2 == 0:
        print(i)
I was expecting this output:
Quote:Secret
agents
good
at

Instead I get this traceback:
TypeError                                 Traceback (most recent call last)
<ipython-input-28-5b7261c4eed7> in <module>()
      3 counted_string = len(mystring)
      4 int(counted_string)
----> 5 for i in counted_string:
      6     if i %2 == 0:
      7         print(i)

TypeError: 'int' object is not iterable
I'm doing something wrong at line 5. The traceback is saying that my counted_string variable is not iterable (because it’s an integer?). Only strings and lists are iterable. So if I substitute counted_string in my for loop with the original mystring variable, I don’t get any output.

Can someone better explain why my code is actually doing and how I can get the desired/expected output?

For my future reference, this forum thread refers to Task #2 in Jose Portilla’s so called 04-Field-Readiness-Exam-2 in his “Narrative Journey” Udemy Python course material (on GitHub: Pierian-Data/Python-Narrative-Journey).
Reply


Messages In This Thread
For loops, strings and printing words with even characters - by Drone4four - Sep-13-2018, 02:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 770 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,777 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,819 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  printing strings with format leodavinci1990 1 1,599 Aug-21-2020, 02:00 AM
Last Post: micseydel
  Introduction to escaping characters in strings Geelong_crazy 1 2,867 Jul-18-2020, 06:58 PM
Last Post: DT2000
  How to find the first and last of one of several characters in a list of strings? tadsss 2 2,205 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,769 May-15-2020, 01:37 PM
Last Post: snippsat
  Trying to get unique words from a set of strings garam0 5 5,175 Apr-15-2020, 06:24 PM
Last Post: garam0
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,799 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Finding multiple strings between the two same strings Slither 1 2,524 Jun-05-2019, 09:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020