0% found this document useful (0 votes)
1 views4 pages

Python String Programs Yash Comments

The document contains a series of Python programs that demonstrate various string manipulation techniques. These include calculating string length, extracting specific characters, modifying characters, removing odd-indexed characters, swapping characters between strings, user input handling, checking string prefixes and suffixes, and formatting strings. Each program is accompanied by comments explaining the logic and methods used.

Uploaded by

ykotwani840
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Python String Programs Yash Comments

The document contains a series of Python programs that demonstrate various string manipulation techniques. These include calculating string length, extracting specific characters, modifying characters, removing odd-indexed characters, swapping characters between strings, user input handling, checking string prefixes and suffixes, and formatting strings. Each program is accompanied by comments explaining the logic and methods used.

Uploaded by

ykotwani840
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Write a Python program to calculate the length of a string using:

a) Length function

b) Any Loop (with comments)

# a) Using len() function

string = "Hello World"

print("Length using len():", len(string)) # Directly returns number of

characters

# b) Using loop with comment

count = 0

for char in string: # Loop through each character

count += 1 # Increment count for each character

print("Length using loop:", count)

2. Write a Python program to get a string made of the first 2 and last 2 characters of a given

string.

If the string length is less than 2, return an empty string ('').

string = "w3resource"

# Check if string length is at least 2

if len(string) >= 2:

result = string[:2] + string[-2:] # Take first 2 and last 2 characters

else:

result = '' # Return empty string if length < 2

print("Result:", result)

3. Write a Python program to get a string where all occurrences of its first char have been

changed to '$', except the first char itself.

string = "google"
first_char = string[0] # Get the first character

# Replace all other occurrences of the first char with '$'

modified = first_char + string[1:].replace(first_char, '$')

print("Modified string:", modified)

4. Write a Python program to remove characters that have odd index values in a given string.

string = "abcdef"

# Method 1: Using slicing to keep even-indexed characters

print("Using slicing:", string[::2])

# Method 2: Using loop with condition

result = ''

for i in range(len(string)):

if i % 2 == 0: # Check if index is even

result += string[i]

print("Using loop:", result)

5. Write a Python program to get a single string from two given strings, separated by a

space, and swap the first two characters of each string.

str1 = "abc"

str2 = "xyz"

# Swap first 2 characters of each string

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

# Combine with space


result = new_str1 + " " + new_str2

print("Resulting string:", result)

6. Write a Python script that takes input from the user and displays that input back in upper

and lower cases.

user_input = input("Enter a string: ") # Take input from user

print("Uppercase:", user_input.upper()) # Convert to uppercase

print("Lowercase:", user_input.lower()) # Convert to lowercase

7. Write a Python program to check whether a string starts with and ends with specified

characters. (Use assigned string 'w3resource.com')

string = "w3resource.com"

start_substring = "w3"

end_substring = ".com"

# Method 1: Using startswith() and endswith()

print("Starts with w3:", string.startswith(start_substring))

print("Ends with .com:", string.endswith(end_substring))

# Method 2: Using slicing to manually check

print("Starts with w3:", string[:len(start_substring)] == start_substring)

print("Ends with .com:", string[-len(end_substring):] == end_substring)

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+)

print(f"My name is {name} and I am {age} years old")

# Method 2: Using format() method

print("My name is {} and I am {} years old".format(name, age))

You might also like