0% found this document useful (0 votes)
4 views8 pages

String Operators

This is my string notes

Uploaded by

mr.upadhyay4091
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

String Operators

This is my string notes

Uploaded by

mr.upadhyay4091
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1

String Operators
>> Definition:
>> String operators are special symbols used to perform operations on
strings such as joining, repeating, and checking for membership.
>> Operator Description Example
>> + Concatenation (join two strings) "Hello" + "World" →
"HelloWorld"
>> * Repetition (repeat string) "Hi" * 3 → "HiHiHi"
>> in Check if substring exists "H" in "Hello" → True
>> not in Check if substring does not exist "z" not in "Hello" → True

a = "Hello"
b = "World"

>> Concatenation
print(a + " " + b) >> Hello World

>> Repetition
print(a * 3) >> HelloHelloHello

>> Membership
print("H" in a) >> True
print("z" not in a) >> True

>> String Modification/ Methods


>> Definition:
2

>> Modification methods create a new string with changes (original


string remains unchanged because strings are immutable).
>> These methods include changing case, capitalizing, swapping case,
and more.

txt = "python programming"

print(txt.upper()) >> PYTHON PROGRAMMING


print(txt.lower()) >> python programming
print(txt.title()) >> Python Programming
print(txt.capitalize()) >> Python programming
print(txt.swapcase()) >> PYTHON PROGRAMMING → python
programming

>> Replace & Join Strings


>> Definition:

>> replace(old, new) → Returns a new string where all occurrences of


old are replaced with new.

>> join(iterable) → Joins elements of a list/tuple into a single string,


separated by the given string.

>> Replace
txt = "I like Java"
print(txt.replace("Java", "Python"))
3

>> Join
words = ["Python", "is", "fun"]
print(" ".join(words)) >> Space-separated
print("-".join(words)) >> Hyphen-separated

>> Searching in Strings


>> Definition:
>> Python provides several methods to search for text inside a string:

>> find(substring) → Returns the index of the first occurrence, or -1 if


not found.

>> index(substring) → Same as find(), but raises an error if not found.

>> startswith(value) → Checks if the string starts with the given value.

>> endswith(value) → Checks if the string ends with the given value.

txt = "I love Python"

>> Find and index


print(txt.find("Python")) >> 7
print(txt.find("Java")) >> -1
print(txt.index("Python")) >> 7
>> print(txt.index("Java")) >> Error if uncommented
4

>> Start and end check


print(txt.startswith("I")) >> True
print(txt.endswith("Python")) >> True

>> String Validation Methods


>> Definition:
>> Validation methods check whether a string contains only certain
types of characters:

>> isalpha() → Only letters.

>> isdigit() → Only digits.

>> isalnum() → Only letters and numbers (no spaces/special chars).

>> isspace() → Only spaces.

>> islower() → All characters lowercase.

>> isupper() → All characters uppercase.

txt = "Rahul123"

print(txt.isalpha()) >> False


print(txt.isdigit()) >> False
5

print(txt.isalnum()) >> True


print("123".isdigit()) >> True
print("hello".isalpha()) >> True
print(" ".isspace()) >> True

txt = "Rahul123"

print(txt.isalpha()) >> False


print(txt.isdigit()) >> False
print(txt.isalnum()) >> True
print("123".isdigit()) >> True
print("hello".isalpha()) >> True
print(" ".isspace()) >> True

txt = "Rahul123"

print(txt.isalpha()) >> False


print(txt.isdigit()) >> False
print(txt.isalnum()) >> True
print("123".isdigit()) >> True
print("hello".isalpha()) >> True
print(" ".isspace()) >> True

txt = "Rahul123"

print(txt.isalpha()) >> False


6

print(txt.isdigit()) >> False


print(txt.isalnum()) >> True
print("123".isdigit()) >> True
print("hello".isalpha()) >> True
print(" ".isspace()) >> Truetxt = "Rahul123"

print(txt.isalpha()) >> False


print(txt.isdigit()) >> False
print(txt.isalnum()) >> True
print("123".isdigit()) >> True
print("hello".isalpha()) >> True
print(" ".isspace()) >> True

name = "Chandresh"
age = 25
print("My name is {} and I am {} years old.".format(name, age))
print("My name is {1} and I am {0} years old.".format(age, name))
>> c) % Formatting (Old Style)
>> Definition:
>> Use % placeholders inside the string (%s for string, %d for integer, %f
for float).
name = "Chandresh"
age = 25
print("My name is %s and I am %d years old." % (name, age))

>> 11. Escape Sequences


7

>> Definition:
>> Escape sequences are special character combinations that represent
actions or characters that are hard to type directly in a string.
>> They start with a backslash (\).
>> | Escape Code | Meaning | Example |
>> | ----------- | --------------- | -------------------------------------- |
>> | `\n` | New line | `"Hello\nWorld"` → prints on two lines
|
>> | `\t` | Tab space | `"Hello\tWorld"` |
>> | `\\` | Backslash | `"This is a backslash: \\"` |
>> | `\'` | Single quote | `'It\'s raining'` |
>> | `\"` | Double quote | `"He said \"Hi\""` |
>> | `\r` | Carriage return | `"Hello\rWorld"` → `Worldo` |
>> | `\b` | Backspace | `"Hello\bWorld"` → `HellWorld` |

print("Name:\tChandresh\nAge:\t25\nCity:\tDelhi")

>> print=("This is a backslash: \\")


>> This is a backslash: \

>> print=("This is a backslash: \\")


>> This is a backslash: \

a = "Hello\tWorld"
print(a)
a = "Hello\tWorld"
print(a)
8

a = "Hello\tWorld"
print(a)
type(print)

>> Double Quote (\")


>> Definition: Allows a double quote inside a double-quoted string.

>> Carriage Return (\r)


>> Definition: Moves the cursor to the beginning of the line and
overwrites existing text.

c=("Helloo\rWorld")
print(c)
>> Backspace (\b)
>> Definition: Deletes the character before it.
x=("Hello\bWorld")
print(x)

You might also like