Unit 4
Unit 4
String Manipulation
string-
Code:
string1 = "hello"
string2 = 'hello'
string3 = '''hello'''
print(string1)
print(string2)
print(string3)
2 – Concatenate Operator “+”
Two strings can be concatenated or joined using the “+” operator in Python, as explained in the below
example code:
Code:
string1 = "hello"
string2 = "world "
string_combined = string1+string2
print(string_combined)
Output:
hello world
3 – String Repetition Operator “*”
The same string can be repeated in Python by n times using string*n, as explained in the below example.
Code:
The “==” operator returns Boolean True if two strings are the same and Boolean False if two strings are different.
The “!=” operator returns Boolean True if two strings are not the same and returns Boolean False if two strings
are the same.
These operators are mainly used along with the if condition to compare two strings where the decision will be
taken based on string comparison.
Code:
string1 = "hello"
string2 = "hello, world"
string3 = "hello, world"
string4 = "world"
print(string1==string4)
print(string2==string3)
print(string1!=string4)
print(string2!=string3)
#6 – Membership Operator “in” & “not in”
The membership operator searches whether the specific character is part/member of a given input
Python string.
“a” in the string: Returns boolean True if “a” is in the string and returns False if “a” is not in the string.
“a” not in the string: Returns boolean True if “a” is not in the string and returns False if “a” is in the string.
A membership operator is also useful to find whether a specific substring is part of a given string.
Code:
string1 = "helloworld"
print("w" in string1)
print("W" in string1)
print("t" in string1)
print("t" not in string1)
print("hello" in string1)
print("Hello" in string1)
print("hello" not in string1)
#7 – Escape Sequence Operator “\”
An escape character is used to insert a non-allowed character in the given input string. An escape character is a
“\” or “backslash” operator followed by a non-allowed character. An example of a non-allowed character in a
Python string is inserting double quotes in the string surrounded by double quotes.
Code:
Code:
Operator Description
In Python, the [:] notation is used to slice a sequence (such as a string, tuple, or list).
len()
lower()
upper()
replace()
join()
split()
find()
isalnum()
isdigit()
isnumeric()
islower()
isupper()
len(string): Returns the length of the string.
string.replace(old, new): Replaces all occurrences of the substring old with new in the string.
string.split(separator): Splits the string into a list of substrings based on the separator.
string.join(iterable): Joins the elements of an iterable (e.g., a list) into a single string using string as a separator.
string.find(substring): Returns the lowest index in the string where substring is found, or -1 if substring is not found.
string.startswith(prefix): Returns True if the string starts with the specified prefix; otherwise, returns False.
string.endswith(suffix): Returns True if the string ends with the specified suffix; otherwise, returns False.
isalnum():- Returns True if all characters in the string are alphanumeric.
isdigit():- Returns True if all characters in the string are digits.
isnumeric():- Returns True if all characters in the string are numeric.
String Method
Count()-The count() method returns the number of occurrences of a substring in the given string.
Casefold()-The casefold() method converts all characters of the string into lowercase letters and returns a new
string.
Index()-The index() method returns the index of a substring inside the string (if found). If the substring is not
found, it raises an exception.
The endswith() method returns True if the string ends with the specified value, otherwise False.
The startswith() method returns True if the string starts with the specified value, otherwise