In: Computer Science
Python please!
Create one program to include all 10 elements:
Create a string of several word and convert it to a list using:
string.split(), use the space to split the words.
Convert the resulting list to the original string using ' '.join(the list)
Program Code Screenshot:
Sample output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program to
copy:
def print_sentence(s):
print("The sentence is :",s)#prints the sentence passed to it
print("The sentence without first and last
characters:",s[1:-1])
print_sentence("This is the sentence")
s="This string was copied in an article"
s=s.replace("copied","discovered")#replace the words and store the
string back to s
s=s.replace("an","the")
s=s.replace("article","article xyz")
print(s)
s="This string is highly reversible"
s=s[::-1]#method 1 to reverse a string
print("The reversed string is:",s)
s="This string is highly reversible"
s="".join(reversed(s))#method 2 to reversed a string
print("The reversed string is:",s)
s="This string has a hidden message in it."
if "hidden" in s:#check if 'hidden' is present in string
print("The word hidden is present in the string")
else:
print("The word hidden is not present in the string")
s="I am not sure what is the my size"
print("The length of the string: ",s," is ",len(s))#print length of
string
s="This sentence needs to get rid of the word"
print("The sentence is:",s)
s=s.replace("to get rid of","")#replace the words
print("After deleting the word from the sentence:",s)
s=" This is a sentence "
print("The sentence after using lstrip() is:",s.lstrip())#removes
the left side spaces
s=" This is a sentence "
print("The sentence after using rstrip() is:",s.rstrip())#removes
the right side spaces
s=" This is a sentence "
print("The sentence after using strip() is:",s.strip())#removes all
the left and right side spaces
def myLineFunction(c,n):
s=c*n
return "The string is:"+s
print(myLineFunction('%', 20))
s="This is a new string"
if s.startswith("This"):
print("The string starts with 'This'")
if s.endswith("ring"):
print("The string ends with 'ring'")
s="This string is converted to list of words"
l=s.split()#split the string to words
print("The list of words:",l)
print("Converting back to string:")
print(' '.join(l))#join the list of words back to string