×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Pattern 04 String Example South point school assignment 16
use of replace() function - Python Language
442    Arnab De    18/01/2021

Use of replace() in Python language

replace() function in python is used for replace the charecter or a set of charecter(called string) with other chracter or set of character of a specified string.

basic Syntax

<string variable name>.replace(<old string>,<new string>)

Example 1

Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '#'.
Sample String : 'array'
Expected Result : '#rr#y'

str1=input("Enter A string : ")
char = str1[0]
str1 = str1.replace(char, '$')
print(str1)

Example 2

Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '#', except the first char itself.
Sample String : 'array'
Expected Result : 'arr#y'

str1=input("Enter A string : ")
char = str1[0]
str1 = str1.replace(char, '$')
str1 = char + str1[1:]
print(str1)
Pattern 04 String Example South point school assignment 16
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Sri Sri Ramakrishna Paramahamsa
If you want to go east, don't go west.
Sri Sri Ramakrishna Paramahamsa
2239
32.45
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     33207
28/06/2017     29017
02/08/2017     26444
26/05/2018     25945
15/05/2017     22743
06/07/2017     21166
01/08/2017     20872
21/04/2018     18372
25/06/2018     17110
14/07/2017     15996
softetechnologies