114k views
4 votes
Python1. Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the last character of the value of name. So if the value of name were "Smith" the expression's value would be "h".2. Given a String variable named sentence that has been initialized, write an expression whose value is the number of characters in the Stringreferred to by sentence.3. Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the last character of the value of name. So if the value of name were "Smith" the expression's value would be "h".

User Gaby Awad
by
3.1k points

1 Answer

5 votes

Answer:

Expression for 1st option:

last_character=name[len(name)-1]

print(last_character)

Expression for 2rd option:

length_of_the_string=len(sentence)

print(length_of_the_string)

or

length=0

for x in sentence:

length=length+1

print(length)

Expression for 3rd option:

last_character=name[len(name)-1]

print(last_character)

Step-by-step explanation:

  • The first and the third questions option are same, so the answer to those options are also the same.
  • The len function is used in python which is used to find the length of the string.
  • The user can also find the string length by the help second option which is written in option 2 answers. It uses a for loop which scans the character and counts the length.
  • The len function defines the length and if any user needs to print the last character then he can do it with the help of string size -1.
  • It is because the string is a collection of character array which index starts from 0 and ends in size-1.
User Adis Azhar
by
3.1k points