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

No comments:

Post a Comment