1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ymorist [56]
4 years ago
5

Write a Python program to replace at most 2 occurrences of space, comma, or dot with a colon. Hint: use re.sub(regex, "newtext",

string, number_of_occurences)
Computers and Technology
1 answer:
charle [14.2K]4 years ago
7 0

Explanation:

The "re" module in python provides various functions to perform complex string operations easily.

For example, you can check whether a specific word exists in a given string or to replace it with some other word.  

Some of the functions in the re module are:

re.findall

re.search

re.sub()

We are particularly interested in the re.sub() function, we can use this function to replace the occurrences of space, coma or dot with semicolon.  

Syntax:

re.sub(pattern, replace, target_string, n).  

Where pattern = space, coma or dot  

replace = semicolon  

target_string = any string containing spaces, coma and dots

n = number of times you want the replacement to be done  

Code:

import re  

target_string = "Hello world,code,code."  

replace = ":"

pattern = "[ ,.]"

print(target_string)

print(re.sub(pattern, replace, target_string, 2) )

Output:

Hello world,code,code.

Hello:world:code,code.

It replaced the first white space and coma with semicolon, if we increase the n to 4 then the output is

Hello:world:code:code:

As expected, it replaced all of the space, coma, and dots with semicolons.

You might be interested in
WILL GIVE BRAIINLIEST AND WATER ICE FROM RITA'S!!!!!!!!!
cricket20 [7]

Answer:

When you purchase a connecting flight, checked baggage is usually forwarded to your final destination, and will change planes when you do. ... In some cases if you purchased multiple tickets that are not all on the same carrier you may need to recheck your baggage

Explanation:

3 0
3 years ago
Which of these purchases is most likely to be paid for with a credit card
Vesna [10]

Answer:

plane ticket?

Explanation:

7 0
3 years ago
"which key do you hold down to select a group of contiguous files"
zloy xaker [14]
The "Ctrl" key is the answer.
4 0
3 years ago
Virtual Memory involves swapping between which 2 devices?
Darina [25.2K]

Answer:

Ram and the hard drive

Explanation:

A page of RAM is written to the harddisk and read back when it is needed again. That way it may seem you have more RAM than you actually have, at the cost of performance of course.

6 0
3 years ago
You want a class to have access to members ofanother class in the same package. Which is themost restrictive access that accompl
sweet-ann [11.9K]

Answer:

default access

Explanation:

Classes, methods, variables(identifiers*) declared without any access modifier are considered as default and these can be accesed in others clases of same  package.

Identifiers are the name of classes, methods, variables, arrays, enums, etc.

3 0
3 years ago
Other questions:
  • Para saan po yung points dito?
    15·1 answer
  • Which of these is a major mobile game developer?
    15·2 answers
  • Could this be restored? And is it worth the money it’d take?
    9·2 answers
  • 2.1 Changes in which factors could cause aggregate demand to shift from AD to AD1? What could happen to the unemployment rate? W
    8·1 answer
  • How can one protect against virus outbreaks in an organization?  What are the common causes of this problems?  How would use add
    15·1 answer
  • I'm programming in javascript, I tryed to put a code like this, but it doesn't work. could someone help me?
    10·1 answer
  • A radio and communications security repairer is responsible for both radio and satellite communication systems.
    6·2 answers
  • Define two benefits to members of the community of replacing the printed copy with an online version.
    5·1 answer
  • _________________: informal messages that are passed on from person to person
    7·1 answer
  • _______________involves engineers building up a 3D geometry piece by piece.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!