Skip to Content
Learn
String Methods
Replace

The next string method we will cover is .replace(). Replace takes two arguments and replaces all instances of the first argument in a string with the second argument. The syntax is as follows

string_name.replace(character_being_replaced, new_character)

Great! Let’s put it in context and look at an example.

with_spaces = "You got the kind of loving that can be so smooth" with_underscores = with_spaces.replace(' ', '_') print(with_underscores) # 'You_got_the_kind_of_loving_that_can_be_so_smooth'

Here we used .replace() to change every instance of a space in the string above to be an underscore instead.

Instructions

1.

The poetry organization has sent over the bio for Jean Toomer as it currently exists on their site. Notice that there was a mistake with his last name and all instances of Toomer are lacking one o.

Use .replace() to change all instances of Tomer in the bio to Toomer. Save the updated bio to the string toomer_bio_fixed.

Folder Icon

Sign up to start coding

Already have an account?