Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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

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

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

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 permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a 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 digital format, then you must 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

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo 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.