Python String Programs Yash Comments
Python String Programs Yash Comments
a) Length function
characters
count = 0
2. Write a Python program to get a string made of the first 2 and last 2 characters of a given
string.
string = "w3resource"
if len(string) >= 2:
else:
print("Result:", result)
3. Write a Python program to get a string where all occurrences of its first char have been
string = "google"
first_char = string[0] # Get the first character
4. Write a Python program to remove characters that have odd index values in a given string.
string = "abcdef"
result = ''
for i in range(len(string)):
result += string[i]
5. Write a Python program to get a single string from two given strings, separated by a
str1 = "abc"
str2 = "xyz"
new_str1 = str2[:2] + str1[2:] # Take first 2 from str2 and rest from str1
new_str2 = str1[:2] + str2[2:] # Take first 2 from str1 and rest from str2
6. Write a Python script that takes input from the user and displays that input back in upper
7. Write a Python program to check whether a string starts with and ends with specified
string = "w3resource.com"
start_substring = "w3"
end_substring = ".com"
8. Write a Python program to format strings in different ways (minimum 2 ways). Input: Name
= Shyam, Age = 30
name = "Shyam"
age = 30
# Method 1: Using f-string (Python 3.6+)