FAQ: String Methods - Replace

This community-built FAQ covers the “Replace” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Replace

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why that ‘’ is used in line one?

A post was split to a new topic: What does /g do?

What if we use .replace() without any argument? What will happen? Like, we can use .split() without any arguments and it works, what is the default argument for .replace()?

There’s no need to guess, best option is just to check the call signature of this method in the docs: https://docs.python.org/3/library/stdtypes.html#str.replace.

You could also just test the result (if you wanted to know why it fails though, see above :slightly_smiling_face:) -

"a string".replace("a")

If I’m being honest I find the python official documentation a little hard to read and understand. Any tips?

You might need a little info about the notation to get started: https://docs.python.org/3/reference/introduction.html#notation (I think this one carries through to the regular documentation too). Otherwise it’s something you’ll get better used to the more you actually use it.

It’s often helpful to run a quick interactive session to test things too, help("".replace) for example should give a quick overview of the method and you can play around with it from there.

I’ve downloaded anaconda but am struggling to use it a little bit - because otherwise I would just ‘play around with it’ as you say. I’ve done a few projects in Jupyter Notebook but that’s different. Spyder seems a bit clustered. What IDE would you recommend from Anaconda?

can

.replace()

return, say, an int representing the number of instances it had to replace? wonder if it could be made to do that…

You could import re and use the re.subn function.

Documentation: https://docs.python.org/3/library/re.html#re.subn

from re import subn

sentence = "Hello There Everyone"

result = subn("e", "E", sentence)
new_sentence, count = result

print(new_sentence)
# "HEllo ThErE EvEryonE"
print(count)
# 5

very interesting, but is it useful?
the question was just me being curious, not like i needed the information, but cool to have!

this is from the project ‘thread shed’
this is getting a little too complicated for its own good isn’t it?


total_sales = 0
for i in sales:
  total_sales += float(i.strip("$"))
thread_sold_split = []
for i in thread_sold:
  l = i.split("&")
  for j in l:
    thread_sold_split.append(j)
    if len(thread_sold_split) > 1:
      n = 0
      while n < len(thread_sold_split) - 1:
        m = n + 1
        while m < len(thread_sold_split) - 1:
          if thread_sold_split[n] == thread_sold_split[m]:
            thread_sold_split.pop(n)
          m += 1
        n += 1