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.
texthas at least one character. It contains only English letters, digits, spaces and punctuation.- Return the first character as a text value of length 1. In Java that means a
String, not achar.
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)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.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