---
title: "Python Strings: Tricky Programs on Remove, Sort, and Count"
description: "The examples showcase Python operations, including string manipulation, list sorting, and character counting, providing practical techniques for beginners."
url: https://srinimf.com/2024/04/16/3-puzzling-python-string-programs-that-look-easy/
date: 2024-04-16
modified: 2026-10-05
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2024/04/pexels-photo-213727.jpeg?fit=1880%2C1253&ssl=1
categories: ["Python"]
tags: ["Python String Tricky Programs", "String Formatting in Python"]
type: post
lang: en
---

# Python Strings: Tricky Programs on Remove, Sort, and Count

Enhance your Python string manipulation skills through valuable code examples, which offer insights into various operations in Python programming.

- It accepts console entries and removes specified characters from them.
- The second program finds the first and second-highest numbers in a list.
- The third program counts consecutive characters in a string.

![Python string manipulation programs](https://i0.wp.com/srinimf.com/wp-content/uploads/2024/04/pexels-photo-552774.jpeg?resize=1747%2C1300&ssl=1)*Photo by Krivec Ales on Pexels.com*

## **01. Remove the Character from the String Entered From the Console**

```
inp = input("Enter the string: ")
ch = input("Enter the character to remove: ")

# Using str.replace() to remove all occurrences of the character
res = inp.replace(ch, "")

print(res)
```

output

```
inp=abc
char entered=a
out=bc
```

## **02. Find first First-highest and Second-Highest**

Use the sorted method to sort in reverse order.

```
l = [10, 90, 40, 60, 20]

# 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
```

## **03. String Character/Word Count**

Example:1 Char count

```
s = "aabbbccccaaaaa"

output = ""
count = 1

for i in range(1, len(s)):
    # Check if the current character is the same as the earlier character
    if s[i] == s[i - 1]:
        count += 1
    else:
        output += s[i - 1] + str(count)
        count = 1

# Add the last character and its count
output += s[-1] + str(count)

print(output)
```

Output

```
a2b3c4a5
```

Example:2 Word count

```
a = "I am here I am fine"

# Split the string into words
words = a.split(" ")

# Initialize an empty dictionary to store word counts
word_count = {}

# Iterate through each word in the list
for word in words:
    # If the word is already in the dictionary, increment its count
    if word in word_count:
        word_count[word] +=1
    # If the word is not in the dictionary, add it with count 1
    else:
        word_count[word] = 1

# Print the word counts
for word, count in word_count.items():
    print(f"{word} - {count}")
```

Output

```
I - 2
am - 2
here - 1
fine - 1


** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Example 3: Char count using collections

```
from collections import Counter

a = "abcabc"
count_dict = Counter(a)
print(count_dict)
out = ""

# Iterate through sorted characters to keep order
for char in sorted(count_dict):
    out += char + str(count_dict[char])

print(out)
```

Output

```
Counter({'a': 2, 'b': 2, 'c': 2})
a2b2c2


** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Example 4: Last word to bring the first position and keep the remaining string the same

```
a="My Name is Srini Here"

b=a.split(" ")

o=b[-1]+ " " + " ".join(b[0:len(b)-1]) 

print(o)
```

Output

```
Here My Name is Srini


** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

**Conclusion**

These python string manipulation programs offer a deeper understanding of list [sorting](https://srinimf.com/2023/12/05/cgi-pyspark-interview-questions-revealed/)and character counting in Python. By working through these examples, you’ll gain practical experience and develop your programming skills.

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2024%2F04%2F16%2F3-puzzling-python-string-programs-that-look-easy%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2024%2F04%2Fpexels-photo-213727.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=Python%20Strings%3A%20Tricky%20Programs%20on%20Remove%2C%20Sort%2C%20and%20Count)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2024/04/16/3-puzzling-python-string-programs-that-look-easy/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2024/04/16/3-puzzling-python-string-programs-that-look-easy/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2024/04/16/3-puzzling-python-string-programs-that-look-easy/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2024/04/16/3-puzzling-python-string-programs-that-look-easy/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Python%20Strings%3A%20Tricky%20Programs%20on%20Remove%2C%20Sort%2C%20and%20Count&body=https%3A%2F%2Fsrinimf.com%2F2024%2F04%2F16%2F3-puzzling-python-string-programs-that-look-easy%2F&share=email)
-

### Like this:

Like Loading…
