If you’re looking to boost your Python programming knowledge, these string manipulation programs can help. By exploring different operations in Python, these code examples offer valuable insights.
- The first program shows how to remove a character from a string using console input.
- The second program teaches how to find the two highest numbers in a list.
- The third program explains how to count consecutive characters in a string.
Table of contents
- Example#1 Remove Char from the Main String That Entered From the Console
- Example#2 Find first First-highest and Second-Highest
- Example#3 String Char Count
- Conclusion
Example#1 Remove Char from the Main String That Entered From the Console
inp = input("Enter the string: ")
ch = input("Enter the character to remove: ")
res = ""
for i in range(len(inp)):
if inp[i] != ch:
res += inp[i]
print(res)
output
inp=abc
char entered=a
out=bc
Example#2 Find first First-highest and Second-Highest
l = [10, 20, 40, 60, 90]
# Sort the list in descending order
sorted_list = sorted(l, reverse=True)
# First highest number
first_highest = sorted_list[0]
# Second highest number
second_highest = sorted_list[1]
print(f"First highest number: {first_highest}")
print(f"Second highest number: {second_highest}")
Output
First heighest: 90
Second heighest: 60
Example#3 String Char Count
input_str = "aabbbccccaaaaa"
output = ""
count = 1
for i in range(len(input_str)):
# Check if the current character is the same as the next character
if i < len(input_str) - 1 and input_str[i] == input_str[i + 1]:
count += 1
else:
output += input_str[i] + str(count)
count = 1
print(output)
Output
a2b3c4a5
Conclusion
These programs provide not only a deeper understanding of string manipulation but also list sorting and character counting in Python. By working through these examples, you’ll gain practical experience and develop your programming skills.
You must be logged in to post a comment.