Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Replace Multiple Spaces with a Single Space | Intermediate Regular Expressions
Python Regular Expressions

bookChallenge: Replace Multiple Spaces with a Single Space

Instructions: Clean Up Extra Spaces with Regex

Text data often contains inconsistent spacing, such as multiple consecutive spaces between words. You need to write a function that uses regex substitution to convert any sequence of multiple spaces into a single space, making the text cleaner and easier to process.

  • Use the re module for your solution;
  • Replace every sequence of two or more consecutive spaces with a single space;
  • Ensure the function returns the cleaned string.
Opgave

Swipe to start coding

You are given a string that may contain extra spaces between words. Your task is to write a function that cleans up the text by replacing every sequence of two or more consecutive spaces with a single space. This will make the text easier to read and process. Use regular expressions to accomplish this.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

Awesome!

Completion rate improved to 6.67

bookChallenge: Replace Multiple Spaces with a Single Space

Stryg for at vise menuen

Instructions: Clean Up Extra Spaces with Regex

Text data often contains inconsistent spacing, such as multiple consecutive spaces between words. You need to write a function that uses regex substitution to convert any sequence of multiple spaces into a single space, making the text cleaner and easier to process.

  • Use the re module for your solution;
  • Replace every sequence of two or more consecutive spaces with a single space;
  • Ensure the function returns the cleaned string.
Opgave

Swipe to start coding

You are given a string that may contain extra spaces between words. Your task is to write a function that cleans up the text by replacing every sequence of two or more consecutive spaces with a single space. This will make the text easier to read and process. Use regular expressions to accomplish this.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
single

single

some-alt