Skip to ContentGo to accessibility page

1.4 String basics

Learning objectives

By the end of this section you should be able to

  • Use the built-in len() function to get a string's length.
  • Concatenate string literals and variables using the + operator.

Quote marks

A string is a sequence of characters enclosed by matching single (') or double (") quotes. Ex: "Happy birthday!" and '21' are both strings.

To include a single quote (') in a string, enclose the string with matching double quotes ("). Ex: "Won't this work?" To include a double quote ("), enclose the string with matching single quotes ('). Ex: 'They said "Try it!", so I did'.

Valid string Invalid string
"17" or '17'
17
"seventeen" or 'seventeen'
seventeen
"Where?" or 'Where?'
"Where?'
"I hope you aren't sad."
'I hope you aren't sad.'
'The teacher said "Correct!" '
"The teacher said "Correct!" "
Table 1.3 Rules for strings.

Concepts in Practice

Valid and invalid strings

1.
Which of the following is a string?
  1. Hello!
  2. 29
  3. "7 days"
2.
Which line of code assigns a string to the variable email?
  1. "fred78@gmail.com"
  2. "email = fred78@gmail.com"
  3. email = "fred78@gmail.com"
3.
Which is a valid string?
  1. I know you'll answer correctly!
  2. 'I know you'll answer correctly!'
  3. "I know you'll answer correctly!"
4.
Which is a valid string?
  1. You say "Please" to be polite
  2. "You say "Please" to be polite"
  3. 'You say "Please" to be polite'

len() function

A common operation on a string object is to get the string length, or the number of characters in the string. The len() function, when called on a string value, returns the string length.

Checkpoint

Using len() to get the length of a string

Using len() to get the length of a string; ch 1, video 6

Concepts in Practice

Applying len() function to string values

5.
What is the return value for len("Hi Ali")?
  1. 2
  2. 5
  3. 6
6.
What is the length of an empty string variable ("")?
  1. undefined
  2. 0
  3. 2
7.
What is the output of the following code?
number = "12"
number_of_digits = len(number)
print("Number", number, "has", number_of_digits, "digits.")
  1. Number 12 has 12 digits.
  2. Number 12 has 2 digits.
  3. Number 12 has number_of_digits digits.

Concatenation

Concatenation is an operation that combines two or more strings sequentially with the concatenation operator (+). Ex: "A" + "part" produces the string "Apart".

Checkpoint

Concatenating multiple strings

Concatenating multiple strings; ch 1, video 7

Concepts in Practice

String concatenation

8.
Which produces the string "10"?
  1. 1 + 0
  2. "1 + 0"
  3. "1" + "0"
9.
Which produces the string "Awake"?
  1. "wake" + "A"
  2. "A + wake"
  3. "A" + "wake"
10.
A user enters "red" after the following line of code.
color = input("What is your favorite color?")
Which produces the output "Your favorite color is red!"?
  1. print("Your favorite color is " + color + !)
  2. print("Your favorite color is " + "color" + "!")
  3. print("Your favorite color is " + color + "!")
11.
Which of the following assigns "one-sided" to the variable holiday?
  1. holiday = "one" + "sided"
  2. holiday = one-sided
  3. holiday = "one-" + "sided"

Try It

Name length

Write a program that asks the user to input their first and last name separately. Use the following prompts (example input in bold):

  What is your first name? Alan
  What is your last name? Turing

The program should then output the length of each name. Based on the example input above, the output would be:

  Your first name is 4 letters long
  Your last name is 6 letters long

Try It

Punctuation

Write a Python computer program that:

  • Assigns the string "Freda" to a variable, name.
  • Assigns the string "happy" to a variable, feel.
  • Prints the string "Hi Freda!" with a single print() function using the variable name.
  • Prints the string "I'm glad you feel happy." with a single print() function using the variable feel.
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.