In: Computer Science
Python please Questions #6 and # 7 please
A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in IndexError exception.
In Python a string literal is defined to be a sequence of characters enclosed in single, double or triple quotes.
To define a string object or variable, we use one of the following:
A string object in immutable, meaning, it cannot be changed once it is defined.
The concatenation operator (+) is used to add two or more string objects (strObj1 + strObj2)
The repetition operator (*) is used to repeat the string object (n*strObj or strObj*n, where n is an integer)
The in and not in operators are used to test if one string object is contained in another string object.
Slice a string object using strObj[start:end], start is the index of the starting character and end-1 is the index last character.
The split method creates a list of the split string object.
lstObj = strObj.split(split-character)
space is the default split-character.
The strip method removes the leading and trailing character, the default is space.
strObj.strip(strip-character)
Variations: rstrip, lstrip (left and right strip methods to remove leading and trailing character, respectively)
Other commonly used string methods are
strObj.lower() changes all characters to lowercase
strObj.upper() changes all characters to uppercase
strObj.islower() tests if all characters are lowercase
strObj.isupper() tests if all characters are uppercase
strObj.replace(old, new) replaces old substring with new substring
strObj.find(substring) returns the lowest index of the substring if it is found in strObj
Activity
Question : Write a program that insert the string objects “of” and “28301” to the new string object .
Answer :
The Code is given below
obj1 = "Of" # String Object obj1
obj2 = "28301" # String Object 2
obj3 = "The price mobile is" # String Object 3
obj3 = "The price {0} mobile is {1}.".format(obj1,obj2) # Inserting two string objects in another(object 3) string
print(obj3)
Output:
The price Of mobile is 28301.
Question 2:Write a program that will display the word, character, space counts in the file storytelling.txt.
def count(fname):
words = 0
charact = 0
spaces = 0
lines = 0
with open(fname,'r') as f:
for line in f:
lines+=1
word = 'Y'
for l in line:
if l!=' ' and word=='Y' :
words +=1
word = 'N'
elif l==' ':
spaces += 1
word = 'Y'
for i in l:
if i!=' ' and i!='\n':
charact +=1
print("Number of Words:",words)
print("Number of Characters : ",charact)
print("Number of SpaceCounts : ",spaces)
# Driver code
if __name__ == '__main__':
fname = 'C:\program.txt'
try:
count(fname)
except:
print("File Not Found")
OUTPUT:
Make Sure that the File in stored in same folder or enter the address of the file carefully
File used in this program is look like
If you get any type of error or any query then write your problem in comment section we will get in touch with you
Thumbs Up if you satisfy
Thankyou