#Display string without considering NULL character
import sys
#Variable Initialization
name1 = 'SANJAY'
#There is no null termination character in python
#Result
sys.stdout.write("Name1 = %s"%(name1))
#Display successive strings
import sys
#Variable Initialization
name1 = 'SANJAY'
name2 = 'SANJAY'
#There is no null termination character in python
#Result
sys.stdout.write("Name1 = %s"%name1)
sys.stdout.write("\nName2 = %s"%(name2))
#Print "WELCOME" by using different formats of initialization
import sys
#Variable Initialization
#in python, string initialization can be done using single quotes and double quotes
arr1 = 'WELCOME'
arr2 = "WELCOME"
arr3 = 'WELCOME'
#Result
sys.stdout.write("\nArray1 = %s"%(arr1))
sys.stdout.write("\nArray2 = %s"%(arr2))
sys.stdout.write("\nArray3 = %s"%(arr3))
#Display the string 'PRABHAKAR' using various format specifications
import sys
#Variable Initialization
text = "PRABHAKAR"
#Result
sys.stdout.write("%s\n"%(text))
sys.stdout.write("%.5s\n"%(text))
sys.stdout.write("%.8s\n"%(text))
sys.stdout.write("%.15s\n"%(text))
sys.stdout.write("%-10.4s\n"%(text))
sys.stdout.write("%11s\n"%(text))
#Print the elements of the character array using while loop.
import sys
#Variable Initialization
text = "HAVE A NICE DAY"
i = 0
#Result
#There is no null character for strings in python
while i < 15:
sys.stdout.write("%c"%(text[i]))
i += 1
#Print the elements of the character array.
import sys
#Variable Initialization
text = "HAVE A NICE DAY"
i = 0
#Result
#There is no null character for strings in python
while i < 15:
sys.stdout.write("%c"%(text[i]))
i += 1
#Count the number of characters in a given string
import sys
#Variable Initialization
text = raw_input("Type Text Below")
#Calculation
len1 = len(text) #since len in a built in function in python, len1 is used
#Result
sys.stdout.write("Length of String = %d"%(len1))
#Determine the length of the string
#and find its equivalent ASCII codes.
import sys
#Variable Initialization
name = raw_input("Enter your name : ")
l = len(name)
#Result
sys.stdout.write("Your Name is %s & "%(name))
sys.stdout.write("it contains %d characters"%(l))
sys.stdout.write("\n\nName & it's Ascii Equivalent.\n")
sys.stdout.write("=====================================\n")
for i in range(0,l):
sys.stdout.write("\n%c\t\t%d"%(name[i],ord(name[i]))) #ord() function is used to get the ASCII value of a character
#Remove the occurrences of 'The' word from entered text
import sys
#Variable Initialization
j = 0
line = raw_input("Enter Text Below.")
l = len(line)
line2 = ['0' for i in range(0,80)]
i = 0
#Calculation
while i < l:
if (i >= l-4 or l ==3) and (line[l-4] == ' ' and line[l-3] == 't' and line[l-2] == 'h' and line[l-1] == 'e'):
continue
if line[i] == 't' and line[i+1] == 'h' and line[i+2] == 'e' and line[i+3] == ' ':
i += 3
continue
else:
line2[j] = line[i]
j += 1
i += 1
#Result
sys.stdout.write("\nText With 'The' -: %s"%(line))
sys.stdout.write("\nText Without 'The' -: ")
for l in range(0,j):
sys.stdout.write("%c"%(line2[l]))
#Delete all the occurrences of vowels in a given text.
import sys
#Variable Initialization
j = 0
line = raw_input("Enter Text Below.")
#Calculation
for i in range(0,len(line)):
if line[i] == 'a' or line[i] == 'e' or line[i] == 'i' or line[i] == 'o' or line[i] == 'u':
continue
else:
line2[j] = line[i]
j += 1
#Result
sys.stdout.write("Text With Vowels -: %s"%(line))
sys.stdout.write("\nText Without Vowels -: ")
for l in range(0,j): #There is no null terminating character in python, so we have to specify length
sys.stdout.write("%c"%(line2[l]))
#Find the length of characteres in a given string including and excluding spaces.
import sys
#Variable Initialization
i = 0
len1 = 0 #Since len is a built in function in python, len1 is used
ex = 0
text = raw_input("Enter Text Here : ")
#Calculation
while i != len(text)-1: #There is no null terminating character in python.
if text[i] == ' ':
ex += 1
else:
len1 += 1
i += 1
#Result
sys.stdout.write("\nLengh of String Including Spaces : %d"%(len1+ex))
sys.stdout.write("\nLengh of String Excluding Spaces : %d"%(len1))
#Display the length of entered string and display it as per output shown.
import sys
#Variable Initialization
string = raw_input("Enter a String")
ln = len(string)
#Result
sys.stdout.write("\nLength of Given String : %d"%(ln))
sys.stdout.write("\nSequence of Characters Displayed on Screen")
sys.stdout.write("\n======== == ========== ========= == ======")
sys.stdout.write("\n\n")
for c in range(0,ln-2+1):
d = c + 1
sys.stdout.write("\t%.*s\n"%(d,string))
for c in range(ln,0-1,-1):
d = c + 1
sys.stdout.write("\t%.*s\n"%(d,string))
#Copy contents of one string to another string by using strcpy()
import sys
#Variable Initialization
ori = raw_input("Enter Your Name : ")
#Process
dup = ori #In python, string values can be simply copied using assignment operator
#Result
sys.stdout.write("Original String : %s"%(ori))
sys.stdout.write("\nDuplicate String : %s"%(dup))
#Copy contents of one string to another, without strcpy() function.
import sys
#Variable Initialization
ori = raw_input("Enter Your Name : ")
#Calculation
dup = ori #In python, string values can be simply copied using assignment operator
#Result
sys.stdout.write("Original String : %s"%(ori))
sys.stdout.write("\nDuplicate String : %s"%(dup))
#Copy source string to destination string upto a specified length.
import sys
#Variable Initialization
str1 = raw_input("Enter Source String : ")
str2 = raw_input("Enter Destination String : ")
n = int(raw_input("Enter Number of Characters to Replace in Destination String :"))
#Calculation
str2 = str1[:n] + str2[n:] #Since python strings are not mutable, first and second array elements to be added to make a new one
sys.stdout.write("Source String -: %s"%(str1))
sys.stdout.write("\nDestination String -: %s"%(str2))
#Compare the two string using stricmp() function.
import sys
#Variable Initialization
sr = raw_input("Enter String(1) :")
tar = raw_input("Enter String(2) :")
diff = (sr == tar) #in python, relational operators can be used directly
if diff == 0:
sys.stdout.write("The Two Strings are Identical.")
else:
sys.stdout.write("The Tow Strings are Different")
#Perform following.
# 1. Display the question "What is the Unit of Traffic?"
# 2. Accept the Answer.
# 3. If the answer is wrong (other than Earlang) display "Try again!" & continues to answer.
# 4. Otherwise, if it is correct "Earlang" display the message "Answer is correct".
# 5. If the user gives correct anser in first two attempts the program will terminate.
# 6. If the user fails to provide correct answer in three attempts the program itself gives the answer.
import sys
#Calculation & Result
for i in range(0,3):
ans = raw_input("What is the Unit of Traffic ?")
if ans == "Earlang":
sys.stdout.write("\nAnswer is Correct.")
else:
if i < 3:
sys.stdout.write("\nTry Again !\n")
if i == 3:
sys.stdout.write("\nUnit of Traffic is Earlang.")
#Compare two strings upto specified length.
import sys
#Variable Initialization
sr = raw_input("Enter String(1):")
tar = raw_input("Enter String(2):")
n = int(raw_input("Enter Lenght upto which comparison is to be made"))
#Calculation & Result
diff = (sr == tar[:n])
if diff == 0:
sys.stdout.write("The Two Strings are Identical upto %d characters."%(n))
else:
sys.stdout.write("The Two Strings are Different")
#Compare two strings and display the position in which it is different.
import sys
#Variable Initialization
diff = 0
sr = raw_input("Enter String(1) :")
tar = raw_input("Enter String(2) :")
#Checking
for i in range(0,len(sr)):
if sr[i] != tar[i]:
sys.stdout.write("%c %c\n"%(sr[i],tar[i]))
diff += 1
#Result
if len(sr) == len(tar) and diff == 0:
sys.stdout.write("\nThe Two Strings are Identical")
else:
sys.stdout.write("\nThe Two Strings are Different at %d places."%(diff))
#Convert upper case string to lower case using strlwr().
import sys
#Variable Initialization
upper = raw_input("Enter a string in Upper case :")
#Result
sys.stdout.write("After strlwr() : %s"%(upper.lower()))
#Convert Lower case string to upper case using strupr()
import sys
#Variable Initialization
upper = raw_input("Enter a string in Lower Case :")
#Result
sys.stdout.write("After strupr() : %s"%(upper.upper()))
#Display the duplicate string using strdup() function
import sys
#Variable Initialization
text1 = raw_input("Enter Text : ")
#Result
text2 = text1 #in python, a string can be duplicated by just using the assignment operator
sys.stdout.write("Original String = %s\nDuplicate String = %s"%(text1,text2))
#Find first occurrence of a given character in a given string.
import sys
#Variable Initialization
string = raw_input("Enter Text Below :")
ch = raw_input("Character to find :")
#Result
chp = string.count(ch)
if chp:
sys.stdout.write("Character %s found in string"%(ch))
else:
sys.stdout.write("Character %s not found in string"%(ch))
#Find first occurrence of a given character in a given string and display the memory location.
import sys
#Variable Initialization
line1 = raw_input("Enter Text :")
line2 = raw_input("Enter Character to find from the text : ")
for i in range(0,len(line1)):
sys.stdout.write("%c %d\n"%(line1[i],id(line1[i])))
chp = line1.count(line2)
if chp:
sys.stdout.write("\nAddress of first %s returned by strchr() is %d"%(line2,id(line1[chp])))
else:
sys.stdout.write("'%s' String is not present in Given String "%(line2))
#Find the occurrence of second string in the first string.
import sys
#Variable Initialization
line1 = raw_input("Enter Line1 :")
line2 = raw_input("Enter Line2 :")
#Calculation & Result
chp = line1.count(line2)
if chp:
sys.stdout.write("'%s' String is present in Given String."%(line2))
else:
sys.stdout.write("'%s' String is not present in Given String."%(line2))
#Append second string at the end of first string using strcat() function
import sys
#Variable Initialization
text1 = raw_input("Enter Text1 :")
text2 = raw_input("Enter Text2 :")
#Calculation and Result
text1 = text1 + " " + text2 #in python, '+' operator can also be used for string concatenation
sys.stdout.write("%s"%(text1))
#Concatenate two strings without use of standard functions.
import sys
#Variable Initialization
fname = raw_input("First Name :")
sname = raw_input("Second Name :")
lname = raw_input("Last Name :")
#Calculation
name = fname + " " + sname + " " + lname
#Result
sys.stdout.write("\nComplete Name After Concatenation.\n")
sys.stdout.write("%s"%(name))
#Append second string with specified (n) number of characters at the end of first string.
import sys
#Variable Initialization
text1 = raw_input("Enter Text1 :")
text2 = raw_input("Enter Text2 :")
n = int(raw_input("Enter Number of Characters to Add :"))
#Calculation & Result
text1 = text1 + " " + text2[:n]
sys.stdout.write("%s\n"%(text1))
#Reverse of the given string.
import sys
#Variable Initialization
text = raw_input("Enter String")
#Result
sys.stdout.write("Reverse String\n")
text = text[::-1]
#This is extended slice syntax. It works by doing [begin:end:step]
#by leaving begin and end off and specifying a step of -1, it reverses a string.
sys.stdout.write("%s"%(text))
#Reverse of the given string.
import sys
#Variable Initialization
text = raw_input("Enter String")
i = 0
#Result
while i < len(text):
sys.stdout.write("\n%c is stored at location %d"%(text[i],id(text[i])))
i += 1
sys.stdout.write("\nReverse String :- ")
text = text[::-1]
#This is extended slice syntax. It works by doing [begin:end:step]
#by leaving begin and end off and specifying a step of -1, it reverses a string.
sys.stdout.write("%s"%(text))
i = 0
while i < len(text):
sys.stdout.write("\n%c is stored at location %d"%(text[i],id(text[i])))
i += 1
#Memory address differs in different systems
#Replace (set) given string with given symbol.
import sys
#Variable Initialization
string = raw_input("Enter String :")
symbol = raw_input("Enter Symbol for Replacement : ")
#Result
sys.stdout.write("Before strset(): %s\n"%(string))
string = symbol*len(string)
sys.stdout.write("After strset() : %s"%(string))
#Replace (set) given string with given symbol for given number of arguments.
import sys
#Variable Initialization
string = raw_input("Enter String :")
symbol = raw_input("Enter Symbol for Replacement : ")
n = int(raw_input("How many String Character to be replaced."))
#Result
sys.stdout.write("Before strset() : %s\n"%(string))
string = symbol * n + string[n:]
sys.stdout.write("After strset() : %s\n"%(string))
#Display the position from which the two strings are different.
import sys
#Variable Initialization
stra = raw_input("First String : ")
strb = raw_input("Second String : ")
#There is no built in function for python to check upto which position the strings are equal.
for length in range(min(len(stra), len(strb))):
if stra[length] != strb[length]:
break
sys.stdout.write("After %d Characters there is no match."%(length))
#Print given string from first occurrence of given character.
import sys
#Variable Initialization
text1 = raw_input("Enter String : ")
text2 = raw_input("Enter Character :")
for length in range(0,len(text1)):
if text1[length] == text2[0]:
break
sys.stdout.write("String from given Character : %s"%(text1[length:]))
#Count a character that appears in a given text
import sys
#Variable Initialization
i = 0
count = 0
text = raw_input("Type Text Below.")
find = raw_input("Type a character to count : ")
#Calculation
while i < len(text): #There is no null terminating character for python string
if text[i] == find:
count += 1
i += 1
#Result
sys.stdout.write("\nCharacter (%s) found in Given String = %d Times."%(find,count))
#Count the character 'm' in a given string.
import sys
#Variable Initialization
count = 0
text = raw_input("") #it is easy to get string at once in python
#Result
sys.stdout.write("%s"%(text))
for i in range(0,len(text)):
if text[i] == '\n':
break
else:
if text[i] == 'm':
count += 1
sys.stdout.write("\n\nCharacter 'm' Found in Text = %d times."%(count))
#Count the characters 'm', 'r', and 'o' that appears in a string without using any functions
import sys
#Variable Initialization
text = "Programming is good habit"
m = 0
o = 0
r = 0
#Calculation
for i in range(0,len(text)):
if text[i] == 'm':
m += 1
if text[i] == 'r':
r += 1
if text[i] == 'o':
o += 1
#Result
sys.stdout.write("\nCharacter 'm' Found in Text = %d times.\n"%(m))
sys.stdout.write("\nCharacter 'r' Found in Text = %d times.\n"%(r))
sys.stdout.write("\nCharacter 'o' Found in Text = %d times."%(o))
#Copy contents of one string to another string
import sys
#Variable Initialization
ori = raw_input("Enter Your Name :")
#Copying
#Since strings in python are immutable, individual character assignment is not possible.
#but assignment operator simply copies the entire string to another
dup = ori
#Result
sys.stdout.write("Original String : %s"%(ori))
sys.stdout.write("\nDuplicate String : %s"%(dup))
#Palindrome or not.
import sys
#Variable Initialization
i = 0
str1 = raw_input("Enter the word : ") #Since str is a keyword in python, str1 is used
j = len(str1)-1
#Calculation
while i < j:
if str1[i] == str1[j]:
test = 1
else:
test = 0
break
i += 1
j -= 1
#Result
if test == 1:
sys.stdout.write("Word is palindrome.")
else:
sys.stdout.write("\nWord is not Palindrome.")
#Compare the string using strcmp() function and display their ASCII difference.
import sys
#Variable Initialization
a1 = "NAGPUR"
a2 = "NAGPUR"
a3 = "PANDHARPUR"
a4 = "KOLHAPUR"
a5 = "AURANGABAD"
a6 = "HYDERABAD"
#Calculation
#There is no built in function which returns the ascii value differece of two strings
#ord function gives the ascii value of a character
c1 = ord(a1[0]) - ord(a2[0])
c2 = ord(a3[0]) - ord(a4[0])
c3 = ord(a5[0]) - ord(a6[0])
#Result
sys.stdout.write("\nAscii Difference between two strings\n")
sys.stdout.write("Difference between (%s %s) = %d\n"%(a1,a2,c1))
sys.stdout.write("Difference between (%s %s) = %d\n"%(a3,a4,c2))
sys.stdout.write("Difference between (%s %s) = %d\n"%(a5,a6,c3))
#Sort city names alphabetically.
import sys
#Variable Initialization
city = [['0' for i in range(5)]for i in range(20)]
sys.stdout.write("Enter Names of Cities. ")
for i in range(5):
city[i] = raw_input("")
#Sorting & Result
sys.stdout.write("Sorted List of Cities.\n\n")
for i in range(65,122+1):
for j in range(0,5):
if ord(city[j][0]) == i:
sys.stdout.write("\n%s"%(city[j]))
#Sort city names alphabetically
import sys
#Variable Initialization
city = [['0' for i in range(5)]for i in range(20)]
sys.stdout.write("Enter Names of Cities. ")
for i in range(5):
city[i] = raw_input("")
#Sorting & Result
sys.stdout.write("Sorted List of Cities.\n\n")
for i in range(0,5):
for j in range(0,5):
if city[j-1] > city[j]:
temp = city[j-1]
city[j-1] = city[j]
city[j] = temp
for i in range(0,5):
sys.stdout.write("\n%s"%(city[i]))
#Find number of words in a given statement.
import sys
#Variable Initialization
count = 1 #Counts the starting word
i = 0
text = raw_input("Enter The Line of Text\nGive One Space After Each Word")
#Calculation
while i < len(text): #There is no null terminating character in python
if ord(text[i]) == 32:
count += 1
i += 1
#Result
sys.stdout.write("\nThe Number of words in line = %d"%(count))
#Find number of words in a given statement.
import sys
#Variable Initialization
count = 0
i = 0
text = raw_input("Enter Text Below :")
#Calculation
while i < len(text): #There is no null terminating character in python for strings
if ord(text[i]) >= 97 and ord(text[i]) <= 122 or ord(text[i]) >= 65 and ord(text[i]) <= 90:
i += 1
continue
if ord(text[i]) == 32:
count += 1
i += 1
continue
if i == len(text):
count += 1
#Result
sys.stdout.write("The Number of words in line = %d"%(count))
#Sort the last name of mobile customers alphabetically.
import sys
#Variable Initialization
fname = [['0' for i in range(20)] for i in range(10)]
sname = [['0' for i in range(20)] for i in range(10)]
surname = [['0' for i in range(20)] for i in range(10)]
name = [['0' for i in range(20)] for i in range(20)]
mobile = [['0' for i in range(20)] for i in range(10)]
temp = ['0' for i in range(20)]
sys.stdout.write("Enter First Name, Second Name, surname and Mobile Number ")
for i in range(0,5):
fname[i] = raw_input("")
sname[i] = raw_input("")
surname[i] = raw_input("")
mobile[i] = raw_input("")
name[i] = surname[i] + " "
temp[0] = fname[i][0]
name[i] = name[i] + temp[0] + "."
temp[0] = sname[i][0]
name[i] = name[i] + temp[0]
for i in range(0,5):
for j in range(1,5-i):
if name[j-1] > name[j]:
temp = name[j-1]
name[j-1] = name[j]
name[j] = temp
temp = mobile[j-1]
mobile[j-1] = mobile[j]
mobile[j] = temp
sys.stdout.write("List of Customers in alphanetical Order.")
for i in range(0,5):
sys.stdout.write("\n%-20s\t %-10s\n"%(name[i],mobile[i]))