Experiment – 1.
2
Student Name : Rahul Sharma UID : 25MAI10008
Branch : ME-CSE(AI&ML) Section : 25MAI-1/A
Semester : 1 Date of Performance : 11/8/24
Subject : Advanced Python Programming Subject Code : 25CSH – 623
1. Aim :
(a) Write a python program to illustrate the concept of range () function.
(b) Write a python program to Demonstrate the use of distinct string functions.
(c) Write a python program to reverse a string without using inbuilt function.
(d) Write a python program to concatenate two strings without using inbuilt function.
2. Task to be done :
(a) Create a Python program that demonstrates how to use the `range()` function in a variety of
loop and sequence configurations.
(b) Create a Python program that demonstrates how certain string methods, such as `upper()`,
`lower()`, and `replace()`, work.
(c) Create a Python program that iterates over a string from beginning to end in order to
manually reverse it.
(d) Create a Python program that iterates through each character in both strings to concatenate them.
3. Software Required :
Python 3 Installed, Code Editor
4. Procedure / Steps:
(a) Steps to Illustrate the Concept of `range()` Function :
1. Setup: Define different scenarios for using the `range()` function (e.g., default range, with
a custom start, and with a step value).
2. Simple `range()`: Use `range(n)` to generate numbers from 0 to `n-1`.
3. Custom Start and Stop: Use `range(start, stop)` to create a sequence from `start` to `stop-1`.
4. Custom Step Value: Use `range(start, stop, step)` to produce numbers with a specified step size.
5. Display Results: Convert the range to a list and print it, or iterate over the range in a `for` loop
to show the sequence.
(b) Steps to Demonstrate the Use of Various String Functions :
1. Setup: Define a sample string to use for testing.
2. Convert to Uppercase: Use `upper()` to change the string to all uppercase letters and print it.
3. Convert to Lowercase: Use `lower()` to change the string to all lowercase letters and print it.
Rahul Sharma 25MAI10008
4. Replace Part of the String: Use `replace(old, new)` to substitute part of the string and display
the result.
5. Find a Substring: Use `find(substring)` to locate the position of a substring and print its index.
6. Split the String: Use `split()` to divide the string into a list based on a delimiter and print the list.
(c) Steps to Reverse a String Without Using Inbuilt Function :
1. Setup: Define the string you wish to reverse.
2. Create an Empty String: Initialize an empty string called `reversed_str`.
3. Loop Through the Original String: Start from the last character and work towards the first.
4. Build the Reversed String: Append each character to `reversed_str`.
5. Display the Result: Print the reversed string.
(d) Steps to Concatenate Two Strings Without Using Inbuilt Function :
1. Setup: Define two strings `str1` and `str2`.
2. Create an Empty String: Initialize an empty string named `concatenated_str`.
3. Loop Through `str1`: Add each character of `str1` to `concatenated_str`.
4. Loop Through `str2`: Add each character of `str2` to `concatenated_str`.
5. Display the Result: Print the combined string.
5. Code and Output :
(a) Write a python program to illustrate the concept of range () function.
# Scenario 1: Default range
n = int(input("Enter a number for the default range: "))
default_range = range(n)
print("Default range:", list(default_range))
# Scenario 2: Custom start and stop start =
int(input("Enter a start value: ")) stop =
int(input("Enter a stop value: "))
custom_range_start_stop = range(start, stop)
print("Custom range with start and stop:", list(custom_range_start_stop))
# Scenario 3: Custom step value
start = int(input("Enter a start value: "))
stop = int(input("Enter a stop value: "))
step = int(input("Enter a step value: "))
custom_range_start_stop_step = range(start, stop, step)
print("Custom range with start, stop, and step:", list(custom_range_start_stop_step))
# Scenario 4: Iterating over the range
n = int(input("Enter a number for the default range: "))
print("Iterating over the range:")
for i in range(n):
print(i)
Output :
Rahul Sharma 25MAI10008
(b) Write a python program to Demonstrate the use of distinct string functions.
# Setup: Define a sample string to use for testing
sample_string = input("Enter a sample string: ")
# 2. Convert to Uppercase uppercase_string =
sample_string.upper()
print("Uppercase string:", uppercase_string)
# 3. Convert to Lowercase lowercase_string =
sample_string.lower()
print("Lowercase string:", lowercase_string)
# 4. Replace Part of the String
old_substring = input("Enter a substring to replace: ")
new_substring = input("Enter a new substring: ")
replaced_string = sample_string.replace(old_substring, new_substring) print("Replaced
string:", replaced_string)
# 5. Find a Substring
substring_to_find = input("Enter a substring to find: ")
index = sample_string.find(substring_to_find)
if index != -1:
print("Substring found at index", index)
else:
print("Substring not found")
Rahul Sharma 25MAI10008
# 6. Split the String
delimiter = input("Enter a delimiter to split the string:
") split_string = sample_string.split(delimiter)
print("Split string:", split_string)
Output :
(c) Write a python program to reverse a string without using inbuilt function.
original_string = input("Enter a string to reverse: ")
reversed_str = ""
for i in range(len(original_string) - 1, -1, -1):
reversed_str += original_string[i]
print("Reversed string:", reversed_str)
Output :
(d) Write a python program to concatenate two strings without using inbuilt function.
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
concatenated_str = ""
Rahul Sharma 25MAI10008
for char in str1:
# Add each character of str1 to concatenated_str
concatenated_str += char
for char in str2:
# Add each character of str2 to concatenated_str
concatenated_str += char
print("Concatenated string:", concatenated_str)
Output :
6. Learning Outcomes :
a. Learn how to create numerical sequences in Python with different start, stop, and step
values using the `range()` function.
b. Recognize and use several string methods for manipulating and analyzing strings, such as
`upper()`, `lower()`, `replace()`, `find()`, and `split()`.
c. Learn how to manually reverse a string by going over each character one by one without the
need for built-in functions.
d. Learn how to manually concatenate two strings by going through each letter in both strings one
at a time.
e. Get hands-on experience creating Python programs that manipulate and perform
standard operations on strings and sequences.
Rahul Sharma 25MAI10008