String¶
STRING DATA TYPE¶
The most commonly used object in any project and in any programming language is String
How to define multi-line String Literals?¶
We can define multi-line String literals by using triple single or double quotes.
multi_line_str = """Any sequence of characters
within either single quotes
or double quotes is considered
as a String."""
print(multi_line_str)
How to Access Characters of a String?¶
We can access characters of a string by using the following ways.
- By using index
- By using slice operator
1) Accessing Characters By using Index¶
- Python supports both +ve and -ve Index.
name = "Ashish Bindra"
print(name[0]) # A
print(name[5]) # h
print(name[-1]) # a
print(name[20])
IndexError: string index out of range
2) Accessing Characters by using Slice Operator¶
- Syntax: name bEginindex:endindex:step]
- Begin Index: From where we have to consider slice (substring)
- End Index: We have to terminate the slice (substring) at endindex-1
- Step: Incremented Value.
str1 = "Learning Python is very very easy!!!"
print(str1[1:7:1]) # earnin
print(str1[1:7]) # earnin
print(str1[1:7:2]) # eri
print(str1[:7]) # Learnin
print(str1[7:]) # g Python is very very easy!!!
print(str1[::]) # Learning Python is very very easy!!!
print(str1[:]) # Learning Python is very very easy!!!
print(str1[::-1]) # !!!ysae yrev yrev si nohtyP gninraeL
Mathematical Operators for String¶
We can apply the following mathematical operators for Strings.
1) * operator for concatenation 2) * operator for repetition
Note¶
1) To use + operator for Strings, compulsory both arguments should be str type. 2) To use * operator for Strings, compulsory one argument should be str and other argument should be int.
Q) Write a Program to access each Character of String in Forward and Backward Direction by using while Loop?¶
Alternative ways:
Checking Membership¶
We can check whether the character or string is the member of another string or not by using in and not in operators.
Comparison of Strings¶
- We can use comparison operators (<, <=, >, >=) and equality operators (==, !=) for strings.
- Comparison will be performed based on alphabetical order.
o/p Enter first string: durga Enter Second string: durga Both strings are equal
Removing Spaces from the String¶
We can use the following 3 methods
1) rstrip(): To remove spaces at right hand side 2) lstrip(): To remove spaces at left hand side 3) strip(): To remove spaces both sides
Finding Substrings¶
We can use the following 4 methods
For forward direction:
1) find() 2) index()
For backward direction:
1) rfind() 2) rindex()
find()¶
s.find(substring)
Returns index of first occurrence of the given substring. If it is not available then we will get -1.
s.find(substring,bEgin,end)¶
It will always search from bEgin index to end-1 index.
index()¶
index() method is exactly same as find() method except that if the specified substring is not available then we will get ValueError.
s=input("Enter main string:")
subs=input("Enter sub string:")
try:
n=s.index(subs)
except ValueError:
print("substring not found")
else:
print("substring found")
o/p Enter main string:learning python is very easy Enter sub string:python substring found
Counting substring in the given String¶
We can find the number of occurrences of substring present in the given string by using count() method.
- s.count(substring): It will search through out the string.
- s.count(substring, bEgin, end): It will search from bEgin index to end-1 index.
s="abcabcabcabcaddab"
print(s.count('a')) # 6
print(s.count('ab')) # 5
print(s.count('abc')) # 4
print(s.count('a',3,7)) # 2
Replacing a String with another String¶
s.replace(oldstring, newstring) inside s, every occurrence of old String will be replaced with new String.
s = "Learning Python is very difficult"
s1 = s.replace("difficult","easy")
print(s1) # Learning Python is very easy
s = "ababababababab"
replace_str = s.replace("a","b")
print(replace_str)
Q) String Objects are Immutable then how we can change the Content by using replace() Method¶
- Once we creates string object, we cannot change the content.This non changeable behaviour is nothing but immutability.
- If we are trying to change the content by using any method, then with those changes a new object will be created and changes won't be happend in existing object.
- Hence with replace() method also a new object got created but existing object won't be changed.
str1 = "abab"
str2 = str1.replace("a","b")
print(str1,"is available at :",id(str1))
print(str2,"is available at :",id(str2))
o/p
Splitting of Strings¶
- We can split the given string according to specified seperator by using split() method.
- l = s.split(seperator)
- The default seperator is space. The return type of split() method is List.
date = "22-07-2024"
date_list = date.split('-')
for val in date_list:
print(val)
# o/p
# 22
# 07
# 2024
Joining of Strings¶
We can join a Group of Strings (List OR Tuple) wrt the given Seperator.
- s = seperator.join(group of strings)
names = ('sunny', 'bunny', 'chinny')
list_name = '-'.join(names)
print(list_name) # sunny-bunny-chinny
Changing Case of a String: We can change case of a string by using the following 4 methods¶
- upper(): To convert all characters to upper case
- lower(): To convert all characters to lower case
- swapcase(): Converts all lower case characters to upper case and all upper case characters to lower case
- title(): To convert all character to title case. i.e first character in every word should be upper case and all remaining characters should be in lower case.
- capitalize(): Only first character will be converted to upper case and all remaining characters can be converted to lower case
- casefold(): Return a version of the string suitable for caseless comparisons.
line = 'learning Python is very Easy'
print(line.upper()) # LEARNING PYTHON IS VERY EASY
print(line.lower()) # learning python is very easy
print(line.swapcase()) # LEARNING pYTHON IS VERY eASY
print(line.title()) # Learning Python Is Very Easy
print(line.capitalize()) # Learning python is very easy
print(line.casefold()) # learning python is very easy
Checking Starting and Ending Part of the String¶
Python contains the following methods for this purpose
- s.startswith(substring)
- s.endswith(substring)
line = 'learning Python is very easy'
print(line.startswith('learning')) # True
print(line.endswith('learning')) # False
print(line.endswith('easy')) # True
To Check Type of Characters Present in a String: Python contains the following methods for this purpose¶
- isalnum(): Returns True if all characters are alphanumeric( a to z , A to Z ,0 to9 )
- isalpha(): Returns True if all characters are only alphabet symbols(a to z,A to Z)
- isdigit(): Returns True if all characters are digits only( 0 to 9)
- islower(): Returns True if all characters are lower case alphabet symbols
- isupper(): Returns True if all characters are upper case aplhabet symbols
- istitle(): Returns True if string is in title case
- isspace(): Returns True if string contains only spaces
Important Programs regarding String Concept¶
Q1) Write a Program to Reverse the given String¶
- Input: durga
- Output: agrud
str1 = input("Enter Some String: ") # Python
i = len(str1) - 1
target=''
while i >= 0:
target = target + str1[i]
i = i - 1
print(target) # nohtyP
"4st way"
s = "hello"
reversed_s = ""
for char in s:
reversed_s = char + reversed_s
print(reversed_s) # Output: "olleh"
"Using a List Comprehension
s = "hello"
reversed_s = ''.join([s[i] for i in range(len(s)-1, -1, -1)])
print(reversed_s) # Output: "olleh"
def reverse_recursive(s):
if len(s) == 0:
return s
else:
return reverse_recursive(s[1:]) + s[0]
s = "hello"
reversed_s = reverse_recursive(s)
print(reversed_s) # Output: "olleh"
from functools import reduce
s = "hello"
reversed_s = reduce(lambda acc, char: char + acc, s)
print(reversed_s) # Output: "olleh"
Q2) Program to Reverse Order of Words¶
- Input: Learning Python is very Easy
- Output: Easy Very is Python Learning
```py "
line=input("Enter Some String: ") # Learning Python is very Easy
list_line = line.split()
l1 = []
i = len(list_line) - 1
while i >= 0: l1.append(list_line[i]) output = ' '.join(l1) i = i - 1
print(output) # Easy very is Python Learning
```py
line=input("Enter Some String: ") # Learning Python is very Easy
list_line = line.split()
list_line.reverse()
print(" ".join(list_line)) # Easy very is Python Learning
Write a Program to find the Number of Occurrences of each Character present in the given String?¶
- Input: ABCABCABBCDE
- Output: A-3,B-4,C-3,D-1,E-1
str1 = input("Enter the Some String: ")
d = {}
for x in str1:
if x in d.keys():
d[x] = d[x] + 1
else:
d[x] = 1
for k, v in d.items():
print(f"{k} = {v} Times")
Assignment Question / Answers¶
66. Python Program to Print Output Without a Newline¶
77. Python Program to Trim Whitespace From a String¶
71. Python Program to Create a Long Multiline String¶
# Example 1: Using triple quotes
my_string = '''The only way to
learn to program is
by writing code.'''
print(my_string)
# Example 2: Using parentheses and a single/double quotes
my_string = ("The only way to \n"
"learn to program is \n"
"by writing code.")
print(my_string)
41. Python Program to Sort Words in Alphabetic Order¶
# Program to sort alphabetically the words form a string provided by the user
my_str = "Hello this Is an Example With cased letters"
# To take input from the user
#my_str = input("Enter a string: ")
# breakdown the string into a list of words
words = [word.lower() for word in my_str.split()]
# sort the list
words.sort()
# display the sorted words
print("The sorted words are:")
for word in words:
print(word)
names = "jashan himanshu ashish"
list_names = names.split(" ")
names = " ".join(sorted(list_names))
print(names)
91. Python Program to Capitalize the First Character of a String¶
67. Python Program to Check If a String Is a Number (Float)¶
def is_float(value):
try:
float(value)
return True
except ValueError:
return False
test_string1 = "123.45"
print(f"Is '{test_string1}' a float? {is_float(test_string1)}")
input_str = input("Enter somthing!! ").strip()
if input_str.isalpha():
print("you enter alphabet which is string!!")
elif input_str.isdigit():
print("String is a int!!")
else:
print("String is float or punctuation!!")
input_str = input("Enter somthing!! ").strip()
try:
input_str = eval(input_str)
if isinstance(input_str,int):
print("you entered int")
elif isinstance(input_str,float):
print("you entered float")
except (NameError,SyntaxError):
print("this is string ")
input_str = input("Enter some string ")
try:
input_str = eval(input_str)
if type(input_str) == type(int()):
print("You entered an int")
elif type(input_str) == type(float()):
print("You entered a float")
except (NameError,SyntaxError):
print("This is string ")
39. Python Program to Check Whether a String is Palindrome or Not¶
s= "level"
if s==s[::-1]:
print('The given string is palindrome')
else:
print('The given string is not palindrome')
# Program to check if a string is palindrome or not
my_str = 'aIbohPhoBiA'
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
my_str = 'aIbohPhoBiA'.lower()
rev_str = ""
for s in my_str:
rev_str=s + rev_str
print(my_str,id(my_str))
print(rev_str,id(rev_str))
if rev_str == my_str:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
my_str = 'pabap'.lower()
if len(my_str)==1 :
print("The string is a palindrome.")
else:
for i in range(0,len(my_str)//2):
if my_str[i] != my_str[-1-i]:
print("The string not is a palindrome.",my_str[-1-i])
break
else:
print("The string is a palindrome.")
40. Python Program to Remove Punctuations From a String¶
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = "Hello!!!, he said ---and went."
# To take input from the user
# my_str = input("Enter a string: ")
# remove punctuation from the string
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
# display the unpunctuated string
print(no_punct)
import string
def remove_punctuation(text):
return text.translate(str.maketrans('', '', string.punctuation))
# Example usage
text = "Hello, World! How's everything?"
cleaned_text = remove_punctuation(text)
print(cleaned_text) # Output: Hello World Hows everything
def remove_punctuation(text):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for char in punctuations:
text = text.replace(char, "")
return text
# Example usage
text = "Hello, World! How's everything?"
print(remove_punctuation(text))
import re
def remove_punctuation(text):
return re.sub(r'[^\w\s]', '', text)
# Example usage
text = "Hello, World! How's everything?"
print(remove_punctuation(text))
43. Python Program to Count the Number of Each Vowel¶
s = "ashish Bindru"
v="aeiou"
for w in v:
print(s.count(w))
# print("The different vowel present in",w,"are",d)
w = "ashish Bindru"
s=set(w)
v={'a','e','i','o','u'}
d=s.intersection(v)
print("The different vowel present in",w,"are",d)
vowels = { "a":0, "e":0, "i":0, "o":0, "u":0 }
s = "ashish Bindru"
vowels = {word: s.count(word) for word in 'aeiou'}
print(vowels)
vowels = { "a":0, "e":0, "i":0, "o":0, "u":0 }
s = "ashish Bindru"
vowels = {word:s.count(word) for word in s if word in vowels}
print(vowels)
vowels = { "a":0, "e":0, "i":0, "o":0, "u":0 }
s = "ashish Bindru"
for word in s:
if word in vowels:
vowels[word]+=1
print(vowels)
# Program to count the number of each vowels
# string of vowels
vowels = 'aeiou'
ip_str = 'Hello, have you tried our tutorial section yet?'
# make it suitable for caseless comparisions
ip_str = ip_str.casefold()
# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
print(count)
# Using a list and a dictionary comprehension
# Using dictionary and list comprehension
ip_str = 'Hello, have you tried our tutorial section yet?'
# make it suitable for caseless comparisions
ip_str = ip_str.casefold()
# count the vowels
count = {x:sum([1 for char in ip_str if char == x]) for x in 'aeiou'}
print(count)
47. Python Program to Create Pyramid Patterns¶
n=7
for i in range(1,n+1):
print(" "*(n-i),end="")
for j in range(1,i+1):
print("*",end=" ")
print()
k = 0
rows = 7
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k += 1
k = 0
print()
61. Python Program to Parse a String to a Float or Int¶
balance_str = "1500"
balance_int = int(balance_str)
# print the type
print(type(balance_int))
# print the value
print(balance_int)
balance_str = "1500.4"
balance_float = float(balance_str)
# print the type
print(type(balance_float))
# print the value
print(balance_float)
balance_str = "1500.34"
balance_int = int(float(balance_str))
# print the type
print(type(balance_int))
# print the value
print(balance_int)
63. Python Program to Convert String to Datetime¶
from datetime import datetime
my_date_string = "Mar 11 2011 11:31AM"
datetime_object = datetime.strptime(my_date_string, '%b %d %Y %I:%M%p')
print(type(datetime_object))
print(datetime_object)
from dateutil import parser
date_time = parser.parse("Mar 11 2011 11:31AM")
print(date_time)
print(type(date_time))
65. Python Program to Get a Substring of a String¶
# Using String slicing
my_string = "I love python."
# prints "love"
print(my_string[2:6])
# prints "love python."
print(my_string[2:])
# prints "I love python"
print(my_string[:-1])
68. Python Program to Count the Occurrence of an Item in a List¶
numbers = [1, 2, 3, 2, 4, 2, 5]
item = 2
count = 0
for element in numbers:
if element == item:
count += 1
print(f"{element_to_count} occurs {count} times in the list.")
numbers = [1, 2, 3, 2, 4, 2, 5]
count_num = set(numbers)
for n in count_num:
if numbers.count(n):
print(f"{n} occures {numbers.count(n)}")
numbers = [1, 2, 3, 2, 4, 2, 5]
l1 = []
l2 = []
for num in numbers:
if num not in l1:
l1.append(num)
else:
l2.append(num)
l1,l2
from collections import Counter
my_list = [1, 2, 3, 4, 2, 2, 5, 6, 5, 3]
occurrences = Counter(my_list)
occurrences
87. Python Program to Reverse a Number¶
90. Python Program to Check If Two Strings are Anagram¶
Two strings are said to be anagrams iff both are having same content irrespective of characters position.
s1 = "papa"
s2 = "appa"
if sorted(s1) == sorted(s2):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
s1 = "".join(sorted(s1))
s2 = "".join(sorted(s2))
if s1 is s2:
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
print(s1,s2)
from collections import Counter
# Function to check if two strings are anagrams
def are_anagrams(str1, str2):
# Anagrams must have the same length
if len(str1) != len(str2):
return False
print(Counter(str1))
# Compare character counts using Counter
return Counter(str1) == Counter(str2)
are_anagrams("lazy","zaly")
92. Python Program to Compute all the Permutation of the String¶
Permutation is the method of selecting elements from a set in different ways.
For example: the number of ways in which characters from yup can be selected are
- yup, ypu, uyp, upy, puy, pyu, and not selecting any
def get_permutation(string, i=0):
if i == len(string):
print("".join(string))
for j in range(i, len(string)):
words = [c for c in string]
# swap
words[i], words[j] = words[j], words[i]
get_permutation(words, i + 1)
print(get_permutation('yup'))
from itertools import permutations
words = [''.join(p) for p in permutations('pro')]
print(words)
94. Python Program to Count the Number of Occurrence of a Character in String¶
count = 0
my_string = "ashish bindra"
my_char = "r"
for i in my_string:
if i == my_char:
count += 1
print(count)
s = "ashish bindra"
d={}
for x in s:
if x in d.keys():
d[x]=d[x]+1
else:
d[x]=1
for k,v in d.items():
print(f"{k} = {v} Times")