7.2.6: First Character CodeHS

"Write a function called first_character that takes a string as a parameter and returns the first character.

Then, write another function called all_but_first_character that takes a string as a parameter and returns everything but the first character.

Test each of your functions on the string "hello" and print the results. Your output should be:"

Respuesta :

def first_character(txt):

   return txt[0]

def all_but_first_character(txt):

   return txt[1::]

print(first_character("hello"))

print(all_but_first_character("hello"))

I wrote my code in python 3.8. I hope this helps.

Following are the Program to the given question:

Program Explanation:

  • Defining a two-method "first_character and all_but_first_character" that takes a string variable "t" into the parameter.
  • In the first method, it returns the first character of the string.
  • In the second method, it returns the string value except for the first character.
  • At the last, we call both the methods.

Program:

def first_character(t):#defining a method first_character that takes a string variable in parameter

  return t[0]#using return keyword that return first character of string

def all_but_first_character(t):#defining a method all_but_first_character that takes a string variable in parameter

  return t[1::]#using return keyword that return string except first character

print(first_character("hello"))#calling method

print(all_but_first_character("hello"))#calling method

Output:

Please find the attached file.

Learn more:

brainly.com/question/19168392

Ver imagen codiepienagoya