Skip to content
ExamSpine

Lesson 5.1 · Write it

First character

A functionPython or Java7 tests, 4 of them hidden

On a phone? Put the lines in order instead

Write a function that returns the first character of a text value.

  1. text has at least one character. It contains only English letters, digits, spaces and punctuation.
  2. Return the first character as a text value of length 1. In Java that means a String, not a char.

Only the value your function returns is checked.

def first_char(text: str) -> str

Examples

Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.

  • first_char("hello") should return "h": the first of five letters (rule 2)
  • first_char("A") should return "A": a single character (rule 1)
  • first_char(" on") should return " ": a space is a character too, and here it is the first one

public static String firstChar(String text)

Checking whether Java answers can run here.

Examples

Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.

  • firstChar("hello") should return "h": the first of five letters (rule 2)
  • firstChar("A") should return "A": a single character (rule 1)
  • firstChar(" on") should return " ": a space is a character too, and here it is the first one
solution.pySolution.java