First steps: programs, values and variables · Part 3 of 36
Working with text: strings, input and output
Basic 14 min
Join, slice and format strings, read input from the user and turn it into numbers safely.
What you will learn
- You can build strings with template literals and f-strings.
- You can take parts of a string by index and slice.
- You can convert user input into a number and handle bad input.
Before this: Variables and data types
Strings are sequences of characters
A string is text: a sequence of characters. You write it in quotes. Both languages accept single or double quotes; JavaScript also has backticks for template literals, and Python has triple quotes for text over several lines.

Putting values into text
Gluing text together with + works but gets messy. Both languages offer a cleaner way:
// JavaScript: template literal with backticks
const name = "Ada";
const age = 36;
console.log(`${name} is ${age} years old`);
# Python: f-string
name = "Ada"
age = 36
print(f"{name} is {age} years old")
Inside ${…} or {…} you can put any expression, for example ${age + 1} or {age + 1}. Python's f-strings can also format numbers: f"{3.14159:.2f}" gives 3.14. In JavaScript you use (3.14159).toFixed(2).
Indexes and slices
Each character has a position, an index, starting at 0.
const word = "garden";
word[0]; // "g"
word.length; // 6
word.slice(1, 4); // "ard" (from index 1 up to, not including, 4)
word.toUpperCase(); // "GARDEN"
word = "garden"
word[0] # "g"
len(word) # 6
word[1:4] # "ard"
word[-1] # "n" (negative index counts from the end)
word.upper() # "GARDEN"
Strings in both languages are immutable: methods such as toUpperCase() or upper() return a new string and leave the original unchanged.
Useful methods you will use constantly: trimming spaces (trim() / strip()), splitting ("a,b".split(",") in both), searching (includes("x") / "x" in s) and replacing (replaceAll / replace).
Reading input
Programs become interesting when they react to the user. In Python on the command line, input("Your age? ") waits for a line of text. In the browser, JavaScript usually reads form fields (input.value); on Node.js you would use the readline module.
Input always arrives as a string, even when the user types digits. Convert it, and plan for bad input:
text = input("How many seeds? ")
try:
seeds = int(text)
except ValueError:
print("Please type a whole number.")
const seeds = Number(text);
if (!Number.isInteger(seeds)) {
console.log("Please type a whole number.");
}
Number("abc") gives NaN ("not a number") in JavaScript instead of an error, so you must check. Never trust input: it can be empty, have spaces, or contain something unexpected entirely — the basis of many security problems you will meet in the expert tier.
Characters are more than bytes
Modern strings are Unicode, a standard that gives a number (a code point) to well over 150,000 characters, including emoji and the scripts of most of the world's languages (Unicode 16.0, September 2024). One visible character can be several code points: an emoji with a skin tone, or "é" written as "e" plus a combining accent. JavaScript's length counts UTF-16 code units, so "😀".length is 2, while Python's len("😀") is 1.
Try it: JavaScript sandbox
Write and run small JavaScript programs; the checker tells you when an exercise is solved.
Turn on JavaScript to use this lab. Worked example:
Exercise 3: given const name = "grove", print GROVE has 5 letters using a template literal, toUpperCase() and length.
Key points
- Strings are immutable sequences of characters; indexes start at 0.
- Template literals (
${x}) and f-strings ({x}) put values into text. - Slices take parts:
s.slice(1, 4)in JavaScript,s[1:4]in Python. - Input is always text: convert it and handle invalid values (
NaN,ValueError). - Strings are Unicode; one visible character can be several code points.
Flashcards
- Question: At which index does the first character of a string sit?
- Answer: Index 0.
- Question: What is an f-string?
- Answer: A Python string with an f prefix where {expressions} are filled in.
- Question: What does Number("abc") return in JavaScript?
- Answer: NaN (not a number).
- Question: Why is "😀".length 2 in JavaScript?
- Answer: length counts UTF-16 code units, and this emoji needs two.
Check yourself
Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.
What does "garden"[1:4] give in Python?
- "ard"
- "gar"
- "arde"
- "rde"
Show the answer
"ard"
The slice starts at index 1 and stops before index 4: a, r, d.
Source: The Python Tutorial, 3.1.2 Text
A JavaScript template literal inserts a value with $___name}.
Show the answer
{
Template literals use ${expression} inside backticks.
Source: MDN, Template literals
What type is the value returned by Python’s input()?
- str
- int
- float
- It depends on what the user types
Show the answer
str
input() always returns a string; you convert it yourself.
Source: The Python Tutorial, built-in input()
How many characters long is the string "skillgrove"?
Show the answer
10
s-k-i-l-l-g-r-o-v-e: 10 characters.
Source: MDN, String length
Sources
- MDN Web Docs JavaScript Guide, Mozilla, 2025
- The Python Tutorial (3.14), Python Software Foundation, 2025
- Unicode Standard, Version 16.0, Unicode Consortium, 2024
Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.