Showing posts with label Python Tips. Show all posts
Showing posts with label Python Tips. Show all posts

Wednesday, 17 June 2026

๐Ÿš€ Day 69/150 – Check Anagram in Python

 



๐Ÿš€ Day 69/150 – Check Anagram in Python

An anagram means two strings contain the same characters in a different order.

✅ Example

listen → silent
race → care

Both words contain the same letters, so they are called anagrams.

๐Ÿ”น Method 1 – Using  sorted()


str1 = "listen"

str2 = "silent" if sorted(str1) == sorted(str2): print("Anagram") else: print("Not Anagram")








✅ Output
Anagram

๐Ÿ“Œ sorted() arranges characters alphabetically and compares both strings.

๐Ÿ”น Method 2 – Taking User Input

str1 = input("Enter first string: ") str2 = input("Enter second string: ") if sorted(str1.lower()) == sorted(str2.lower()): print("Anagram") else: print("Not Anagram")








✅ Example Output
Enter first string: Heart
Enter second string: Earth

Anagram

๐Ÿ“Œ lower() ignores uppercase and lowercase differences.


๐Ÿ”น Method 3 – Using Dictionary Count

str1 = "race"

str2 = "care" count1 = {} count2 = {} for ch in str1: count1[ch] = count1.get(ch, 0) + 1 for ch in str2: count2[ch] = count2.get(ch, 0) + 1 if count1 == count2: print("Anagram") else: 






print("Not Anagram")

Output

Anagram

๐Ÿ“Œ This method compares the frequency of each character.


๐Ÿ”น Method 4 – Using Function

def is_anagram(str1, str2): return sorted(str1.lower()) == sorted(str2.lower()) print(is_anagram("listen", "silent"))





✅ Output
True

๐Ÿ“Œ Functions make the code reusable and cleaner.


๐Ÿ”ฅ Key Takeaways

✅ Anagrams contain the same characters in different order
✅ sorted() is the easiest and most popular method
✅ Dictionary counting helps understand character frequency
✅ lower() avoids case mismatch problems
✅ Anagram problems are common in coding interviews

Monday, 15 June 2026

๐Ÿš€ Day 68/150 – Replace Characters in String in Python

 


๐Ÿš€ Day 68/150 – Replace Characters in String in Python

Sometimes we need to replace characters or words inside a string.

Example:
"Python" → Replace "P" with "J" → "Jython"

Python makes this super simple using different methods ๐Ÿ‘‡

๐Ÿ”น Method 1 – Using  replace()

text = "Python"

result = text.replace("P", "J") print(result)





✅ Output
Jython

๐Ÿ“Œ replace() replaces all matching characters or words in a string.


๐Ÿ”น Method 2 – Taking User Input

text = input("Enter a string: ") old_char = input("Enter character to replace: ") new_char = input("Enter new character: ") result = text.replace(old_char, new_char) print("Updated String:", result)









✅ Example Output
Enter a string: banana
Enter character to replace: a
Enter new character: o

Updated String: bonono

๐Ÿ“Œ Useful when replacement values come from the user.


๐Ÿ”น Method 3 – Using for Loop

text = "apple" result = "" for ch in text: if ch == "p": result += "b" else: result += ch print(result)











✅ Output
abble

๐Ÿ“Œ This method manually checks every character and replaces matching ones.


๐Ÿ”น Method 4 – Using List Comprehension

text = "hello" result = "".join(["*" if ch == "l" else ch for ch in text]) print(result)






✅ Output
he**o

๐Ÿ“Œ A compact and Pythonic way to replace characters.


๐Ÿ”ฅ Key Takeaways

✅ replace() is the simplest method
✅ Loops help understand string manipulation logic
✅ List comprehension makes code shorter and cleaner
✅ String replacement is useful in text processing and cleaning
✅ Strings are immutable, so replacement creates a new string



Sunday, 14 June 2026

๐Ÿš€ Day 67/150 – Remove Spaces from String in Python

 


๐Ÿš€ Day 67/150 – Remove Spaces from String in Python

Sometimes while working with strings, we need to remove spaces from text data.

Example:
"Python Programming" → "PythonProgramming"

Python provides multiple easy ways to do this. Let’s explore them ๐Ÿ‘‡

๐Ÿ”น Method 1 – Using replace()

text = "Python Programming"

result = text.replace(" ", "") print(result)






✅ Output
PythonProgramming

๐Ÿ“Œ replace() replaces all spaces with an empty string.


๐Ÿ”น Method 2 – Taking User Input

text = input("Enter a string: ") result = text.replace(" ", "") print("After Removing Spaces:", result)





✅ Example Output

Enter a string: Python Programming
After Removing Spaces: PythonProgramming

๐Ÿ“Œ Useful when working with user-entered text.


๐Ÿ”น Method 3 – Using split() and join()

text = "Python Programming" result = "".join(text.split()) print(result)






✅ Output
PythonProgramming

๐Ÿ“Œ split() separates words and join() combines them without spaces.


๐Ÿ”น Method 4 – Using for Loop

text = "Python Programming" result = "" for ch in text: if ch != " ": result += ch print(result)









✅ Output
PythonProgramming

๐Ÿ“Œ This method manually checks each character and skips spaces.


๐Ÿ”ฅ Key Takeaways

✅ replace() is the easiest method
✅ split() + join() is clean and efficient
✅ Loops help understand string manipulation logic
✅ Removing spaces is useful in text cleaning tasks

Friday, 12 June 2026

๐Ÿš€ Day 64/150 – Count Vowels in a String in Python

 


๐Ÿš€ Day 64/150 – Count Vowels in a String in Python

Vowels are the letters: a, e, i, o, u.
In Python, we can count vowels in a string using loops, list comprehensions, and functions.

Let’s explore different ways to count vowels in Python ๐Ÿ‘‡

๐Ÿ”น Method 1 – Using for Loop

text = "Python Programming" count = 0 for ch in text.lower(): if ch in "aeiou": count += 1 print("Vowel Count:", count)









✅ Output
Vowel Count: 4

๐Ÿ“Œ Loops through each character and increases the count when a vowel is found.


๐Ÿ”น Method 2 – Taking User Input

text = input("Enter a string: ") count = 0 for ch in text.lower(): if ch in "aeiou": count += 1 print("Vowel Count:", count)









๐Ÿ“Œ Allows the user to enter any string dynamically.

๐Ÿ”น Method 3 – Using List Comprehension

text = "Python Programming" count = sum([1 for ch in text.lower() if ch in "aeiou"]) print("Vowel Count:", count)



✅ Output

Vowel Count: 4

๐Ÿ“Œ Short and Pythonic way to count vowels.

๐Ÿ”น Method 4 – Using Function

def count_vowels(text): count = 0 for ch in text.lower(): if ch in "aeiou": count += 1 return count print(count_vowels("Python Programming"))







✅ Output

4

๐Ÿ“Œ Best approach when you want reusable code.


๐Ÿ”ฅ Key Takeaways

✅ lower() helps handle uppercase and lowercase letters
✅ in "aeiou" is an easy way to check vowels
✅ List comprehensions make code shorter
✅ Functions improve reusability and readability



Popular Posts

Categories

100 Python Programs for Beginner (119) AI (282) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (11) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (31) data (6) Data Analysis (36) Data Analytics (23) data management (15) Data Science (370) Data Strucures (22) Deep Learning (178) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (73) Git (11) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (317) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1379) Python Coding Challenge (1164) Python Mathematics (1) Python Mistakes (51) Python Quiz (543) Python Tips (10) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)