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
#2
(Sep-13-2018, 02:05 PM)Drone4four Wrote:
mystring = "Secret agents are super good at staying hidden."
mystring.split()

Strings are immutable objects - meaning that they cannot be changed "in-place". You essentially create a temporary list object without assigning the result - so that list is lost

(Sep-13-2018, 02:05 PM)Drone4four Wrote:
counted_string = len(mystring)
int(counted_string)
Result of len function is integer - and the second line again does nothing

(Sep-13-2018, 02:05 PM)Drone4four Wrote:
for i in counted_string:    
    if i %2 == 0:
        print(i)

In Python, you iterate over lists directly; if you need index - you use enumerate function; if you want to iterate over range of values - you use range function (just to explain your mistake, don't need it here)

This video is highly recommended for those learning how to loop in Python
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 793 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,807 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,848 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  printing strings with format leodavinci1990 1 1,605 Aug-21-2020, 02:00 AM
Last Post: micseydel
  Introduction to escaping characters in strings Geelong_crazy 1 2,877 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,214 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,835 May-15-2020, 01:37 PM
Last Post: snippsat
  Trying to get unique words from a set of strings garam0 5 5,196 Apr-15-2020, 06:24 PM
Last Post: garam0
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,807 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Finding multiple strings between the two same strings Slither 1 2,536 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