Python Beginner needs help with for loop

T

ty.kresher

Hi, I'm trying to write a for loop in place of the string
method .replace() to replace a character within a string if it's
found.

So far, I've got

s = raw_input("please enter a string: ")
c = raw_input("please enter a character: ")
b_s = s[::-1] #string backwards

found = False #character's existence within the string

for i in range(0,len(s)):
if(b_s == c):
print "the last occurrence of %s is in position: " % (c),
print (len(s)-i)-1 #extract 1 for correct index
found = True
break #run just once to print the last position
if found:
s2 = s.replace(c,"!")
print "the new string with the character substituted is: " +
s2

I need to replace s2 = s.replace(c,"!") with a for loop function
somehow.

I don't really see how a for loop would iterate through the string and
replace the character, and would like to see an example if possible.
 
G

Gabriel Genellina

En Sun, 10 Feb 2008 04:06:54 -0200, (e-mail address removed)
I need to replace s2 = s.replace(c,"!") with a for loop function
somehow.

I don't really see how a for loop would iterate through the string and
replace the character, and would like to see an example if possible.

Try starting with an empty string, and building the output one character
at a time (s = s + x); when you are at the position to be replaced, use
the replacement character instead.
 
E

endangeredmassa

Hi, I'm trying to write a for loop in place of the string
method .replace() to replace a character within a string if it's
found.

So far, I've got

s = raw_input("please enter a string: ")
c = raw_input("please enter a character: ")
b_s = s[::-1] #string backwards

found = False #character's existence within the string

for i in range(0,len(s)):
if(b_s == c):
print "the last occurrence of %s is in position: " % (c),
print (len(s)-i)-1 #extract 1 for correct index
found = True
break #run just once to print the last position
if found:
s2 = s.replace(c,"!")
print "the new string with the character substituted is: " +
s2

I need to replace s2 = s.replace(c,"!") with a for loop function
somehow.

I don't really see how a for loop would iterate through the string and
replace the character, and would like to see an example if possible.



s.replace(c,"!") will replace all instances of that character. What
you need to do is just create a new string that equals the old one,
but the target character is replaced with the replacement character.

You don't have to set a flag for "found" at all. You can do it all in
that for loop. Here's an example.

FOR LOOP
IF MATCH
CONSTRUCT NEW STRING WITH REPLACEMENT
SET NEW STRING TO OLD STRING

I can provide actual python code as well.
 
P

Paul Rubin

Hi, I'm trying to write a for loop in place of the string
method .replace() to replace a character within a string if it's
found.

I think you mean you want to replace the last occurrence of the
character in the string, but leave other occurrences intact.
So far, I've got

s = raw_input("please enter a string: ")
c = raw_input("please enter a character: ")

So far so good
b_s = s[::-1] #string backwards
found = False #character's existence within the string

I wouldn't bother with this
for i in range(0,len(s)):

I'd use the built-in rindex function to find the last index:

loc = s.rindex(c)

Note this will raise an exception if c isn't found. If you don't
want that, use rfind instead of rindex. rfind will return -1 and
then you'll have to check for that.
print "the last occurrence of %s is in position: " % (c),
print (len(s)-i)-1 #extract 1 for correct index

You can put both expressions in the same formatted string:

print "the last occurrence of %s is in position %d: " % (c, loc)
s2 = s.replace(c,"!")
print "the new string with the character substituted is: " + s2

s2 = '%s!%s'% (s[:loc], s[loc+1:])
 
T

ty.kresher

I'm replacing all occurrences of the character in the string with '!',
which in turn is the new string, s2.

I'm also not allowed to use rfind or rindex or replace() because we
are supposed to be learning about for loops.

Thanks for the advices so far, I will try some more on my own.
 
C

Chris

Hi, I'm trying to write a for loop in place of the string
method .replace() to replace a character within a string if it's
found.

So far, I've got

s = raw_input("please enter a string: ")
c = raw_input("please enter a character: ")
b_s = s[::-1] #string backwards

found = False #character's existence within the string

for i in range(0,len(s)):
    if(b_s == c):
        print "the last occurrence of %s is in position: " % (c),
        print (len(s)-i)-1 #extract 1 for correct index
        found = True
        break #run just once to print the last position
if found:
    s2 = s.replace(c,"!")
    print "the new string with the character substituted is: " +
s2

I need to replace s2 = s.replace(c,"!") with a for loop function
somehow.

I don't really see how a for loop would iterate through the string and
replace the character, and would like to see an example if possible.


Do the character checking and replacement in the same loop.

s = raw_input("please enter a string: ")
c = raw_input("please enter a character: ")
new_string = ''
replacement_character = '!'
found = False
for each_char in s[::-1]:
if found:
new_string += each_char
elif not found and each_char == c:
new_string += replacement_character
found = True
else:
new_string += each_char

print 'New string is : %s' % new_string[::-1]

PS: String addition is not the most efficient thing in the world.
 
J

John Machin

Hi, I'm trying to write a for loop in place of the string
method .replace() to replace a character within a string if it's
found.
So far, I've got
s = raw_input("please enter a string: ")
c = raw_input("please enter a character: ")
b_s = s[::-1] #string backwards
found = False #character's existence within the string
for i in range(0,len(s)):
if(b_s == c):
print "the last occurrence of %s is in position: " % (c),
print (len(s)-i)-1 #extract 1 for correct index
found = True
break #run just once to print the last position
if found:
s2 = s.replace(c,"!")
print "the new string with the character substituted is: " +
s2

I need to replace s2 = s.replace(c,"!") with a for loop function
somehow.
I don't really see how a for loop would iterate through the string and
replace the character, and would like to see an example if possible.

Do the character checking and replacement in the same loop.

s = raw_input("please enter a string: ")
c = raw_input("please enter a character: ")
new_string = ''
replacement_character = '!'
found = False
for each_char in s[::-1]:
if found:
new_string += each_char
elif not found and each_char == c:


The "not found and" could be removed; it's quite redundant
new_string += replacement_character
found = True
else:
new_string += each_char

But don't stop there; the whole contents of the loop could be replaced
by:

if not found and each_char == c:
new_string += replacement_character
found = True
else:
new_string += each_char
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top