๐Ÿ
B.TECH I YEAR โ€ข II SEMESTER โ€ข R25

Python Programming - Lab Manual

Python Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š II Semester ๐Ÿ Python Programming ๐Ÿงช Lab Program
Selected Program

sentence as an input parameter and replaces the first letter

โ† All Python Programs

Aim:

☛   Write a function that takes a sentence as an input parameter and replaces the first letter of every word with the corresponding upper case letter and the rest of the letters in the word by corresponding letters in lower case without using a built-in function?

Solution :


PROGRAM: (replace_first.py)

 
def replace_first(str_in):
	x = list(str_in)
	i = 0
	while i < len(x):
		# consider first character of word
		if i == 0 :
			if ord(x[i]) >= 97 and ord(x[i]) <= 122 :
				x[i] = chr(ord(x[i]) - 32)
		elif x[i] == ' ':
			i = i + 1
			if ord(x[i]) >= 97 and ord(x[i]) <= 122 :
				y = ord(x[i])
				x[i] = chr(y - 32)
		else :
			if ord(x[i]) >= 65 and ord(x[i]) <= 92 :
				x[i] = chr(ord(x[i]) + 32)
		i = i + 1
	str_out = ' '
	for j in x :
		str_out = str_out + j
	return str_out

str_in = input(" Enter any string = ")
str_op = replace_first(str_in)
print(str_op)

OUTPUT:

 
 Enter any string = welcome TO STUdy glance
 Welcome To Study Glance


๐Ÿ’ป Run This Python Program

sentence as an input parameter and replaces the first letter
The selected Python program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server.
โ† Back to Python Lab Programs ๐Ÿ“š All Lab Manuals