Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

1.4: String Basics

( \newcommand{\kernel}{\mathrm{null}\,}\)

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!
  • 29
  • "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
  • 5
  • 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
  • "1 + 0"
  • "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.

    This page titled 1.4: String Basics is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?