PYTHON
Practice Set-03(String)
String:
In Python, a string is a sequence of characters
enclosed in quotes. It can include letters, numbers,
symbols or spaces. Since Python has no separate
character type, even a single character is treated as
a string of length. Strings are widely used for text
handling and manipulation.
Creating a String
Strings can be created using either single
('...') or double ("...") quotes. Both behave the
same.
Example:
s1 = ‘UIT' # single quote
s2 = “UIT" # double quote
print(s1)
print(s2)
Accessing
characters in String:
0 1 2 3 4 5 6
a b c d e f g
s = “abcdefg"
print(s[0])
print(s[4])
s = “abcdefg"
print(s[-1])
print(s[-5])
String Slicing
Slicing is a way to extract portion of a string by
specifying the start and end indexes. The syntax
for slicing is string[start:end],
where start starting index and end is stopping
index (excluded).
Example:
s = “united collage"
print(s[1:4]) # characters from index 1 to 3
print(s[:3]) # from start to index 2
print(s[3:]) # from index 3 to end
print(s[::-1]) # reverse string
String Iteration:
Strings are iterable; you can loop through
characters one by one.
Example:
s = "Python"
for char in s:
print(char)
String Immutability:
Strings are immutable means that they cannot
be changed after they are created. If we need
to manipulate strings then we can use methods
like concatenation, slicing or formatting to
create new strings based on original.
Example:
s = “united"
s = “U" + s[1:] # create new string
print(s)
Deleting a String:
In Python, it is not possible to delete individual
characters from a string since strings are
immutable. However, we can delete an entire
string variable using the del keyword.
Example:
s = “UIT"
del s
Updating a String:
As strings are immutable, “updates” create new
strings using slicing or methods such as replace
().
Example:
s = "hello UIT"
s1 = "H" + s[1:]
s2 = [Link](“UIT", “UGI")
Common String
Methods:
1. len(): The len() function returns
the total number of characters in a
string (including spaces and
punctuation).
Example:
s = “UIT"
print(len(s))
2. upper() and
lower(): upper() method converts
all characters to uppercase whereas,
lower() method converts all characters
to lowercase.
Example:
3. strip() and replace(): strip() removes
leading and trailing whitespace from the string
and replace() replaces all occurrences of a
specified substring with another.
Example:
s = " UIT "
print([Link]())
s = "Python is fun"
print([Link]("fun", "awesome"))
capitalize(): Capitalizes the first character.
title(): Converts to title case (first letter of
each word capitalized).
swapcase(): Swaps the case of all
characters.
string built in functions
and methods:
Concatenating and
Repeating Strings:
We can concatenate strings
using + operator and
repeat them using * operator.
1. Strings can be combined
by using + operator.
Example: s1 = "Hello"
s2 = "World"
print(s1 + " " + s2)
2. We can repeat a string
multiple times using *
operator.
Example: s = "Hello "
print(s * 3)
String Comparison in
Python:
1)== Operator for
Equality Check
The == operator is a
simple way to check if two
strings are identical. If both
strings are equal, it returns
True; otherwise, it returns
False.
Example:
s1 = "Python"
s2 = "Python"
# Since both strings are
identical, therefore it is
True
print(s1 == s2)
2)!= Operator for Inequality Check:
The != operator helps to verify if two strings
are different. If the strings are different then
it will return True, otherwise returns False.
Example:
s1 = "Python"
s2 = "Java"
# "Python" is different from "Java", therefore
it is True
print(s1 != s2)
3)Lexicographical Comparison
Lexicographical comparison checks if one
string appears before or after another in
alphabetical order. This is especially useful for
sorting.
Example:
s1 = "apple"
s2 = "banana"
# "apple" appears before "banana" alphabetically,
therefore it is True
print(s1 < s2)
# "banana" comes after "apple", therefore it is
True
print(s2 > s1)
String Question:
Write a program to calculate the length of string
without using built in function.
Write a program to convert upper case letters to
lower case
WAP to Reverse a String.
WAP to Check for Palindrome.
WAP to Count Vowels and Consonants in a String.
WAP to Remove Duplicate Characters from a String.
WAP to Check if Two Strings are Anagrams.
WAP to Find First Non-Repeating Character.
WAP to Group Anagrams from a List of Strings.
12)WAP to Check if a String Can Be
Rearranged into a Palindrome:
Input: “civic”
Output: True
13)WAP to Longest Substring Without
Repeating Characters
Input: "abcabcbb"
Output: 3