replace function in python

 replace function:

python having different types of in-built functions from them replace() is one of the function.
  • The replace() function name itself saying that "one string is replaced with the other string".
  • It consists of three parameters old value, new value, and count.
old value:  The value that we want to replace.

new value: The value we want to replace with is the new value.

count: The number of times of the value we want to replace old with the new value.

Syntax: str.replace(old value,new value,count)

#write a python code to demonstrate replace function.

  1. a="my name is java"
  2. b=" she is a very talented person"
  3. c="They have no knowledge in python"
  4. a=a.replace("java","python")
  5. b=b.replace("talented person","brave girl")
  6. c=c.replace("no","more",3)
  7. print(a)
  8. print(b)
  9. print(c)
  • In the above program in line 5, we want to replace no with more and we gave the count as 3 so in the string where we have no that will be replaced with more up to 3 times only.
  • so the output will be as shown in below.
Output:  my name is python
               she is a very brave girl
              They have more kmorewledge in python 



strip function in python


In Python, the str.strip() method is used to remove leading and trailing whitespaces from a string. By default, the strip() method removes any whitespace characters from the beginning and end of the string, such as spaces, tabs, and newlines. 
The strip() function is a string method that removes leading and trailing whitespace characters from a string. By default, it removes spaces, tabs, and newline characters from the beginning and end of a string. 
However, it can also be used to remove any specified characters from the beginning and end of a string.
Here's an example of strip() method:

It's important to note that the strip() function returns a new string and does not modify the original string.
We can also use lstrip() and rstrip() to remove leading and trailing whitespaces respectively.
strip() function in Python is a useful method for removing unwanted characters from the beginning and end of a string, which helps in cleaning up the data and making it ready for further processing or analysis.

original_string = "   Hello World   "
stripped_string = original_string.strip()
print(stripped_string)

This will print "Hello World" with leading and trailing whitespaces removed.

In addition to removing whitespaces, the strip() method can also remove specific characters from the beginning and end of the string. You can pass one or more characters as arguments to the strip() method, and it will remove those characters from the beginning and end of the string. Here's an example:


original_string = "!!Hello World!!"
stripped_string = original_string.strip('!')
print(stripped_string)

This will print "Hello World" with the leading and trailing '!' characters removed.

Alternatively, you can use lstrip() and rstrip() method to remove leading and trailing whitespaces respectively.


original_string = "   Hello World   "
stripped_string = original_string.lstrip()
print(stripped_string)

This will print "Hello World " with leading whitespaces removed.


original_string = "   Hello World   "
stripped_string = original_string.rstrip()
print(stripped_string)

This will print " Hello World" with trailing whitespaces removed.

It's worth noting that, the strip() method returns a new string with the leading and trailing whitespaces removed, it does not modify the original string.
  • In python, we are having another in-built function called "strip()".
  • The strip() function is used to remove the spaces of the starting and end of the string.
  • It is having one parameter character used to remove the begging or end character.
  • By default, it removes the white spaces at the beginning or end.
  • If there is no white space between the beginning or end, the string remains the same and returns the same string.
  • When the character we give does not match the start or end, it returns the same string.
  • If the character is given at start to remove then the space will be removed and the remaining will remain the same.
syntax: str. strip(char)

#write a python program to demonstrate the strip function()

  1. txt1="   my name is python"
  2. txt2="***my name is python***"
  3. txt3=",,,my name is python"
  4. txt1=txt1.strip()
  5. txt2=txt2.strip("*")
  6. txt3=txt3.strip(".")
  7. print(txt1)
  8. print(txt2)
  9. print(txt3)
Output: my name is python
               my name is python
               my name is python

length of string python

 length of the string:

  • The len() function gives the length of the string.
  • As it returns the result in integer type.
  • It takes the string as the parameter.
  • We can use len() for list,tuple,set,dictionry.
  • The len() is the built-in function in python programming.
  • Example:  "string"  its length is  6.
Syntax: str(length)

#write a python program to demonstrate the length of the string.

  1. string="america"
  2. print(len(string))
Output: 7


concat python

 concatenation:

  • In python, concatenation is defined as "the process of combining or joining or merging one string with the other string".
  •  It uses the plus operator for combining the strings.
  • The string must be enclosed with single quotations or double.
  • Type conversion is needed.
  • We can combine an integer value with a string by using the str(int) function.
  • The plus(+) operator is used to combine the string at the end of the string.
  • The % string format operator is used to combine the string anywhere we want.
Syntax: str1+str2

#write a python program to demonstrate concatenation function.

  1. a="python"
  2. b="programming"
  3. x=a+b
  4. print(x)
  5. str1="python"
  6. str2=str(3.0)
  7. str=str1+str2
  8. print(str)
Output: pythonprogramming
              python3.0



startswith python

  •  In python, we have another built-in method called startswith.
  • startswith is used to check whether the string startswith this specified string.
  • It returns the boolean type.
  • If the string startswith gave specified string then it returns true else false.
Syntax: str.startswith(value,start,end)

  • It consists of the 3 parameters.
  1. value:  The value that the string starts with.
  2. start:    The position to start the search of string.
  3. end:  The position to end the search.

#write a python program to demonstrate the startswith method.

  1. str="welcome to python"
  2. str=str.startswith("w",0,14)
  3. print(str)
Output:true

strftime python

 strftime :

  •  The method represents the string consists of data and time with objects as time and date or date time.
  • The datetime class is imported from the date time module.
  • It takes one or more input format codes and returns string representation.
Syntax: strftime(format)

  • The list of format codes are:
  1. %a:  It is used for weekdays representation in short forms as wed, thr, etc...
  2. %A: It is used for weekdays representation in the full name as of Sunday, Monday.
  3. %b: It is used for month name representation in short form as of Jan, Feb, etc
  4. %B: It is used for month name representation in the full name as of April, May, etc.
  5. %d: It is used as a representation of the day of the month as zero added.
  6.  %-d: day of the month as a decimal number.
  7. %m: Month as zero added decimal number. 
  8. %-m: Month as a decimal number.
  9. %H:  hours as zero added decimal number.
  10. %-H: hours as a decimal number.
  11. %M: minutes as zero added decimal number.
  12. %-M: minutes as a decimal number.
  13. %S: seconds as a zero added decimal number
  14. %-S: seconds as a decimal number.
  15. %J: day of the year as zero added decimal number.
  16. %-J: day of the year as a decimal number.

#write a python program to demonstrate strftime function.

  1. from datetime import datetime
  2. now=datetime.now()
  3. year=now.strftime("%Y")
  4. print("year:",year)
  5. month=now.strftime("%m")
  6. print("month:",month)
  7. day=now.strftime("%d")
  8. print("day:",day)
  9. time=now.strftime("%H:%M:%S")
  10. print("time:",time)
  11. date_time=now.strftime("%m%d%Y, %H%M%S")
  12. print("date and time:",date_time)
output:
year:2021
month:07
day:05
time:10:50:36
date and time:07052021,105036



Select Menu