(for python)
plaintext = "SNOWDENHASTHEDOCUMENTS"
a) write an expression that returns the number of characters in plaintext
b) write an expression that returns the number of substring "DEN" of the string plaintext
c) write an expression that returns the reversed string
a)
Python provides len() function to calculate length of a string.
Argument to the length function is a variable which stored the string
Expression: len(plaintext)
b)
Count() function is returns the number of occurrences of a substring in a given string.
Syntax: stringvariable.count("substring")
Expression: plaintext.count("DEN")
c)There are many ways to reverse a string.Best way is to use extended sliced index.
Expression: plaintext[ : : -1]
Extended slice syntax provides a “step” field as [start,stop,step].
Giving no field as start and stop indicates default to 0 and string length respectively and “-1” denotes starting from end and stop at the start, which reverses the string.
Get Answers For Free
Most questions answered within 1 hours.