Total Pageviews

Saturday, April 18, 2020

STRING HANDLING IN PYTHON

📘 String Handling in Python 


🎯 What is String Handling?

String handling in Python means performing operations like:

  • creating strings
  • modifying strings
  • searching
  • replacing
  • splitting/joining
  • formatting text

📌 Definition

String handling refers to all operations performed on strings using built-in functions and methods to process and manipulate text data.


🧵 STRING IN PYTHON (RECAP)

s = "Python Programming"
  • Strings are immutable (cannot be changed directly)
  • Stored as sequence of characters
  • Indexed from 0

🔥 IMPORTANT STRING METHODS (WITH PROGRAM + OUTPUT)


1. upper()

👉 Convert to uppercase

s = "python"
print(s.upper())

Output:

PYTHON

2. lower()

👉 Convert to lowercase

s = "PYTHON"
print(s.lower())

Output:

python

3. title()

👉 First letter of each word capital

s = "python programming language"
print(s.title())

Output:

Python Programming Language

4. capitalize()

👉 First letter of sentence capital

s = "python is easy"
print(s.capitalize())

Output:

Python is easy

5. strip()

👉 Remove spaces from both sides

s = "   Python   "
print(s.strip())

Output:

Python

6. lstrip() / rstrip()

s = "   Python   "
print(s.lstrip())
print(s.rstrip())

Output:

Python   
Python

7. replace()

👉 Replace words

s = "I like Java"
print(s.replace("Java", "Python"))

Output:

I like Python

8. find()

👉 Find index of word

s = "Python Programming"
print(s.find("Pro"))

Output:

7

9. index()

s = "Python"
print(s.index("t"))

Output:

2

10. count()

s = "banana"
print(s.count("a"))

Output:

3

11. split()

👉 Convert string into list

s = "I love Python"
print(s.split())

Output:

['I', 'love', 'Python']

12. join()

👉 Join list into string

words = ["I", "love", "Python"]
print(" ".join(words))

Output:

I love Python

13. len()

s = "Python"
print(len(s))

Output:

6

14. startswith()

s = "Python Programming"
print(s.startswith("Python"))

Output:

True

15. endswith()

s = "Python Programming"
print(s.endswith("ing"))

Output:

True

16. isalpha()

s = "Python"
print(s.isalpha())

Output:

True

17. isdigit()

s = "12345"
print(s.isdigit())

Output:

True

18. isalnum()

s = "Python123"
print(s.isalnum())

Output:

True

19. swapcase()

s = "PyThOn"
print(s.swapcase())

Output:

pYtHoN

20. center()

s = "Python"
print(s.center(10, "*"))

Output:

**Python**

📌 IMPORTANT STRING HANDLING OPERATIONS


🔹 1. Concatenation

a = "Hello"
b = "World"
print(a + " " + b)

Output:

Hello World

🔹 2. Repetition

s = "Hi "
print(s * 3)

Output:

Hi Hi Hi 

🔹 3. Indexing

s = "Python"
print(s[0])

Output:

P

🔹 4. Slicing

s = "Python"
print(s[0:4])

Output:

Pyth

📘 SUMMARY

String handling includes:
✔ case conversion (upper, lower)
✔ searching (find, index)
✔ modification (replace, strip)
✔ splitting/joining
✔ checking methods (isalpha, isdigit)
✔ formatting operations


🧠 IMPORTANT EXAM QUESTIONS

🔹 Short Questions

  1. What is string handling?
  2. What is immutable string?
  3. Difference between find() and index()
  4. What is split() used for?
  5. What is join()?

🔹 Long Questions

  1. Explain string handling in Python
  2. Write programs using string methods
  3. Explain all string functions
  4. Difference between upper() and lower()
  5. Explain split and join with example

NESTED FOR LOOP IN PYTHON

📘 Nested for Loop in Python (Easy Explanation + Examples)


🎯 What is Nested for Loop?

A nested for loop means a for loop inside another for loop.

It is used when we need to work with:

  • patterns
  • tables
  • matrices
  • grids
  • repeated combinations

📌 Definition

A nested for loop is a loop where one for loop is placed inside another for loop to perform repeated iteration of iterations.


🧠 Syntax

for i in range(n):
for j in range(m):
# code block

🔁 How It Works

  • Outer loop runs first
  • For each outer loop, inner loop runs completely
  • Then outer loop moves to next iteration

📊 Example 1: Simple Nested Loop

for i in range(1, 4):
for j in range(1, 4):
print(i, j)

Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

📊 Example 2: Multiplication Table

for i in range(1, 6):
for j in range(1, 11):
print(i * j, end=" ")
print()

📊 Example 3: Star Pattern (Rectangle)

for i in range(1, 5):
for j in range(1, 6):
print("*", end=" ")
print()

Output:

* * * * *
* * * * *
* * * * *
* * * * *

📊 Example 4: Right Angle Triangle Pattern

for i in range(1, 6):
for j in range(1, i+1):
print("*", end=" ")
print()

Output:

*
* *
* * *
* * * *
* * * * *

📊 Example 5: Number Pattern

for i in range(1, 5):
for j in range(1, i+1):
print(j, end=" ")
print()

Output:

1
1 2
1 2 3
1 2 3 4

📊 Example 6: Matrix Printing

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

for i in matrix:
for j in i:
print(j, end=" ")
print()

⚡ Advantages of Nested for Loop

  • Useful for patterns
  • Works with matrices
  • Handles multiple iterations
  • Very important for exams

❌ Disadvantages

  • Can be slow for large data
  • Complex if deeply nested
  • Hard to debug

🧠 Real-Life Examples

  • Chess board ♟️
  • Calendar system 📅
  • Table/grid display 📊
  • Matrix operations 🧮

📘 Summary

  • Nested for loop = loop inside loop
  • Outer loop controls rows
  • Inner loop controls columns
  • Used in patterns, tables, matrices

🧠 Important Questions

Short Questions

  1. What is nested for loop?
  2. Write syntax of nested for loop.
  3. Give one example.
  4. What is outer loop?
  5. What is inner loop?

Long Questions

  1. Explain nested for loop with example.
  2. Write program for star pattern.
  3. Write program for multiplication table using nested loop.
  4. Explain matrix printing using nested loop.
  5. Write number pattern program.





📘 20 Pattern Programs Using Nested for Loop (Python)

These are very important exam + viva + coding questions.


⭐ 1. Square Pattern

for i in range(5):
for j in range(5):
print("*", end=" ")
print()

⭐ 2. Right Triangle Star Pattern

for i in range(5):
for j in range(i+1):
print("*", end=" ")
print()

⭐ 3. Reverse Right Triangle

for i in range(5, 0, -1):
for j in range(i):
print("*", end=" ")
print()

⭐ 4. Number Triangle

for i in range(1, 6):
for j in range(1, i+1):
print(j, end=" ")
print()

⭐ 5. Same Number Triangle

for i in range(1, 6):
for j in range(i):
print(i, end=" ")
print()

⭐ 6. Continuous Number Pattern

num = 1
for i in range(4):
for j in range(4):
print(num, end=" ")
num += 1
print()

⭐ 7. Inverted Number Triangle

for i in range(5, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()

⭐ 8. Pyramid Star Pattern

for i in range(5):
print(" " * (5 - i), end="")
for j in range(i + 1):
print("* ", end="")
print()

⭐ 9. Inverted Pyramid

for i in range(5, 0, -1):
print(" " * (5 - i), end="")
for j in range(i):
print("* ", end="")
print()

⭐ 10. Diamond Pattern (Simple)

for i in range(5):
print(" " * (5 - i), end="")
for j in range(i + 1):
print("* ", end="")
print()

for i in range(4, 0, -1):
print(" " * (5 - i), end="")
for j in range(i):
print("* ", end="")
print()

⭐ 11. Alphabet Pattern (A)

for i in range(5):
for j in range(i+1):
print(chr(65 + j), end=" ")
print()

⭐ 12. Repeated Alphabet

for i in range(5):
for j in range(i+1):
print(chr(65 + i), end=" ")
print()

⭐ 13. Floyd’s Triangle

num = 1
for i in range(5):
for j in range(i+1):
print(num, end=" ")
num += 1
print()

⭐ 14. 0-1 Pattern

for i in range(5):
for j in range(i+1):
print((i + j) % 2, end=" ")
print()

⭐ 15. Hollow Square

n = 5

for i in range(n):
for j in range(n):
if i == 0 or i == n-1 or j == 0 or j == n-1:
print("*", end=" ")
else:
print(" ", end=" ")
print()

⭐ 16. Hollow Triangle

for i in range(5):
for j in range(i+1):
if j == 0 or j == i or i == 4:
print("*", end=" ")
else:
print(" ", end=" ")
print()

⭐ 17. Right Pascal Triangle

for i in range(5):
for j in range(i+1):
print("*", end=" ")
print()

for i in range(4, 0, -1):
for j in range(i):
print("*", end=" ")
print()

⭐ 18. Left Triangle

for i in range(5):
print(" " * (5 - i), end="")
for j in range(i+1):
print("*", end=" ")
print()

⭐ 19. Number Pyramid

for i in range(5):
print(" " * (5 - i), end="")
for j in range(i+1):
print(j+1, end=" ")
print()

⭐ 20. Full Pyramid Numbers

for i in range(1, 6):
print(" " * (5 - i), end="")
for j in range(1, i+1):
print(j, end=" ")
for j in range(i-1, 0, -1):
print(j, end=" ")
print()

NESTED IF IN PYTHON

1. POSITIVE,NEGATIVE ZERO CHECKING - CLICK


📘 Nested if in Python (Easy Explanation + Examples)


🎯 What is Nested if?

A nested if means an if statement inside another if statement.

It is used when we need to check multiple conditions step by step.


📌 Definition

Nested if is an if statement written inside another if block to check additional conditions.


🧠 Syntax

if condition1:
if condition2:
# code block
else:
# code block
else:
# code block

📊 Example 1: Check Number is Positive and Even

n = 10

if n > 0:
if n % 2 == 0:
print("Positive Even Number")
else:
print("Positive Odd Number")
else:
print("Negative Number")

📊 Example 2: Login System

username = "admin"
password = "1234"

if username == "admin":
if password == "1234":
print("Login Successful")
else:
print("Wrong Password")
else:
print("Invalid Username")

📊 Example 3: Largest of 3 Numbers

a = 10
b = 20
c = 15

if a > b:
if a > c:
print("A is largest")
else:
print("C is largest")
else:
if b > c:
print("B is largest")
else:
print("C is largest")

📊 Example 4: Voting Eligibility

age = 20
citizen = True

if age >= 18:
if citizen:
print("Eligible to vote")
else:
print("Not a citizen")
else:
print("Not eligible")

📊 Example 5: Grade System

marks = 85

if marks >= 50:
if marks >= 80:
print("Grade A")
else:
print("Grade B")
else:
print("Fail")

⚡ Why Use Nested if?

  • To check multiple conditions step by step
  • To make decision trees
  • To handle complex logic

❌ Disadvantages

  • Code becomes complex
  • Hard to read if too many levels
  • Can be replaced with elif sometimes

🆚 Nested if vs elif

FeatureNested ifelif
StructureInside another ifSame level
ComplexityHighLow
ReadabilityLowerBetter
UsageMultiple dependent checksMultiple independent checks

📌 Real-Life Example

  • Bank login system 🏦
  • Exam result system 📊
  • Eligibility checking 🎓
  • ATM PIN verification 💳

📘 Summary

  • Nested if means if inside if
  • Used for multiple condition checking
  • Helps in decision-making programs
  • Should be used carefully to avoid confusion

🧠 Important Questions

Short Questions

  1. What is nested if?
  2. Give syntax of nested if.
  3. Where is nested if used?
  4. Difference between if-elif and nested if.
  5. Give one example.

Long Questions

  1. Explain nested if with example.
  2. Write program for login system using nested if.
  3. Write program for largest of three numbers.
  4. Explain advantages and disadvantages of nested if.
  5. Write program for grading system using nested if.