Skip to lesson
Python for JS Developers

Syntax BridgeLesson 2 of 4

Strings & f-strings

4 min

If you love template literals in JS, you are going to love f-strings in Python even more. They work similarly (prefix the string with f and put expressions in curly braces), but f-strings can do more: you can format numbers, align text, and call functions inside the braces.

The string methods are where you will stumble most. Names you have memorized for years are different: toUpperCase() becomes upper(), trim() becomes strip(), includes() becomes the in operator. The good news: Python's string methods are generally shorter and more consistent.

One conceptual point: Python strings are immutable (just like JS), and Python has no separate char type. A single character is just a string of length 1.

A few things JS developers rarely think about: single quotes and double quotes are completely identical in Python, including for f-strings. Pick one style and be consistent (most teams pick double quotes, which is also what the Black and Ruff formatters use). Triple quotes (three single or double quotes) create multi-line strings, similar to JS template literals but without interpolation unless you add the f prefix.

Raw strings are important to learn early. Prefix a string with r (like r"\n") and Python will not interpret backslash sequences, so r"\n" is a backslash followed by an n, not a newline. This is essential for regex patterns (r"\d+") and Windows file paths (r"C:\Users\docs"). Without the r prefix, you would need to double every backslash.

Quick reference

JavaScriptPythonNote
`Hello ${name}`f"Hello {name}"f-prefix, either quote style
.toUpperCase().upper()Shorter name
.toLowerCase().lower()Shorter name
.trim().strip()Different name
.includes("x")"x" in sin operator, not method
"ha".repeat(3)"ha" * 3Multiplication operator
.indexOf("x").find("x") / .index("x").find() returns -1, .index() raises
arr.join(", ")", ".join(lst)join is a string method

Side by side

JavaScript
// JavaScript
const name = "Alice";
const age = 25;
console.log(`Hello ${name}, age: ${age}`);

"hello world".toUpperCase();
"hello world".includes("world");
"hello world".split(" ");
"  spaces  ".trim();
"ha".repeat(3);
["a", "b"].join(", ");
Python
# Python
name = "Alice"
age = 25
print(f"Hello {name}, age: {age}")
print(f"Next year: {age + 1}")  # expressions!
print(f"{name:>20}")            # right-align in 20 chars

"hello world".upper()
"world" in "hello world"   # 'in' operator!
"hello world".split(" ")
"  spaces  ".strip()       # strip, not trim
"ha" * 3                   # *, not .repeat()
", ".join(["a", "b"])      # separator.join(list)

Exercise

Real Python, running in your browser

Write Python code that does the following: 1. Variable name = any name string 2. Variable lang = 'Python' 3. Print a greeting using an f-string with both variables 4. Print the same greeting in uppercase

Expected output
Welcome Alice, hello to Python!
WELCOME ALICE, HELLO TO PYTHON!

Ctrl + Enter runCtrl + Shift + Enter checkTab indentEsc then Tab to leave the editorCtrl + / comment