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
What are two examples of ways an electronic record may be distributed to others?
Karolina [17]

Answer:

Way 1. Email, Way 2. Publish on Internet

Explanation:

8 0
3 years ago
Interpret the poem 'the popular field'as a definse of natural conservation​
olga2289 [7]

Answer:

<em><u>Consider the poem “The Poplar Field” as a defense of nature conservation. This poem is a defense of nature conservation. The poet's message is that trees shouldn't be cut down recklessly. ... The poet also says that though we can plant trees, they don't grow as fast as the trees are being cut down in the present time.</u></em>

Explanation:

7 0
3 years ago
________ enables advertisers to display more relevant ads based on user's search and browsing history. FIP NORA A Web bug An int
Arturiano [62]

Answer:

Behavioural targeting

Explanation:

Behavioural targeting is the method that enables advertisers to display more relevant ads based on user's search and browsing history. This is an online targeting technique that collects information from a particular user. This information comes from the person's browsing history. The information that comes from the browsing history enable advertisers to display specific targeted ads that would most appeal to the person in question.

7 0
3 years ago
Q3: Create a class called Employee that includes three instance variables—a first name (typeString), a last name (type String) a
ser-zykov [4K]

Answer:

Follows are the code to the given question:

public class Employee //defining a class Employee

{

String first_name,last_name;

Double monthly_salary;

Employee(String first_name,String last_name,Double monthly_salary)

{

   this first_name=f_name// initialize value in first_name

   this last_name = l_name; // initialize value in the last_name

    this monthly_salary = m_salary; // initialize value in the monthly_salary

if (monthly_salary < 0.0)//use if to check monthly_salary is positive

           monthly_salary = 0.0;//set value 0

}

void setfirst_name (String f_name)//defining set method to store first_name

{

first_name = f_name; //holding the value of first_name

}  

public String getfirst_name ()//defining get method to return first_name

{

return first_name;//return first_name

}  

public void setlast_name (String l_name)//defining set method to store last_name

{

last_name = l_name; // holding the value of the last_name

}

public String getlast_name ()//defining get method to return last_name

{

return last_name;//return last_name

}  

public void setmonthly_salary(double m_salary)//defining set method to store monthly_salary

{

monthly_salary = m_salary; // holding the value of the monthly_salary

}

public double getmonthly_salary ()//defining get method to return monthly_salary

{

return monthly_salary;//return monthly_salary  

}

}

Explanation:

In this code, a class "Employee" is declared that defines two string variables  "first_name,last_name", and one double variable "monthly_salary". In the next step, a parameterized constructor is defined that stores the value into the variable, and use if block to check "monthly_salary" is a positive. It also uses the getter and setter method to hold and return its value.  

   

5 0
3 years ago
Which design principle indicates that a project is visually satisfying
joja [24]

Answer: harmony

Explanation:

Took the test on gmetrix ;)

6 0
3 years ago
Other questions:
  • Kerning is the adjustment of space between ___ characters.
    11·1 answer
  • Which feature is a component of a database application?
    7·1 answer
  • Complete the sentence about a focusing technique.
    7·1 answer
  • the header, in an academic report, typically contains the author’s name and the current page number true or false
    6·1 answer
  • Complete the printtictactoe function with char parameters horizchar and vertchar that prints a tic-tac-toe board with the charac
    15·1 answer
  • The set of instructions that tells a computer
    12·2 answers
  • Two Smallest (20 points). Write a program TwoSmallest.java that takes a set of double command-line arguments and prints the smal
    7·1 answer
  • What is the difference between master file and transaction file​
    10·1 answer
  • Which type of information should never be given out on social media?
    5·2 answers
  • What file system supported by windows can be used for volumes and drives that don’t hold the windows installation? (choose all t
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!