Programming fundamentalsLesson 2 of 3 in the course Statements

Python String Literals, Length, and Concatenation

5 flashcards · 5 quiz questions · 5 match pairs · 5 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

A string is text represented as a sequence of characters. In Python, strings are written inside matching single or double quotation marks. This lesson introduces string quotation, measuring string length with len(), and combining strings with +.

A string literal is text written directly in a program and enclosed by a matching pair of quotation marks. Python accepts either single quotes ('...') or double quotes ("...").

All flashcards

What is a string?
A string is a sequence of characters represented in Python by text enclosed in matching single or double quotation marks.
How do you include an apostrophe in a string easily?
Enclose the string in double quotes, as in `"Won't this work?"`, so the apostrophe does not end the string.
What does the len() function do?
`len()` returns the number of characters in a string. It counts spaces and punctuation as characters.
What is string concatenation?
Concatenation is joining strings sequentially. In Python, the `+` operator combines strings, but it does not add spaces unless they are included in the strings.
How can a variable be included in a printed message?
Concatenate string literals and the string variable with `+`, then pass the resulting expression to `print()`, such as `print("Hi " + name + "!")`.

Quiz questions

  1. What makes a value a string literal in Python?Answer: It is text enclosed by a matching pair of single or double quotation marks.
  2. What does len("Hi!") return?Answer: 3, because the string contains H, i, and !.
  3. Which string correctly includes the apostrophe in the word "Won't"?Answer: "Won't"
  4. What is the result of "A" + "part"?Answer: "Apart"
  5. How can a program print "Hi Freda!" using the variable name assigned the string "Freda"?Answer: Use `print("Hi " + name + "!")`.

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons