String Operations in Python

Python allows several operations on strings like joining, repeating, checking membership, and comparing. These are useful for performing various tasks while handling text.

Types of String Operations:

  1. Concatenation
  2. Repetition
  3. Membership Operators
  4. String Comparison

1. Concatenation

Concatenation means joining two or more strings together. It uses the + (plus) operator to merge strings.

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)

Output: Hello World

Explanation:

  • The + operator joins the strings.
  • A space " " is added manually between the two words.

2. Repetition

Repetition means repeating the same string multiple times. It uses the * (asterisk) operator.

text = "Hi "
print(text * 3)

Output: Hi Hi Hi

Explanation:

  • The string "Hi " is printed 3 times.
  • Repetition operator helps in patterns or repeated text output.

3. Membership Operators

Membership operators check whether a substring exists in the main string or not. It uses in and not in keywords.

message = "Python is fun"
print("fun" in message)
print("easy" not in message)

Output:
True
True

Explanation:

  • "fun" in message is True because "fun" is present.
  • "easy" not in message is True because "easy" is missing.

4. String Comparison

String comparison means checking whether two strings are equal, or which one is greater alphabetically. In Python, we use comparison operators to compare strings. It uses operators like ==, !=, >, <, etc.

Example:

a = "apple"
b = "banana"

print(a == b)
print(a < b)

Output:
False
True

Explanation:

  • a == b gives False because the strings "apple" and "banana" are not the same.
  • a < b gives True because in alphabetical (dictionary) order, "apple" comes before "banana".
  • Python compares each character using their Unicode values. First letters 'a' and 'b' are compared. Since 'a' comes before 'b', the result is True.

Important Points:

  • String comparison is case-sensitive. "Apple" and "apple" are considered different.
  • Strings are compared based on dictionary (alphabetical) order.

Summary:

  1. + joins strings (concatenation).
  2. * repeats the string multiple times.
  3. in / not in checks if substring exists.
  4. ==, !=, >, < used for comparing strings.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar