Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Creating and Inspecting Strings | Strings
Data Types in Python

bookCreating and Inspecting Strings

Strings are how Python stores text - names, messages, file paths, even emoji. In Python 3, strings are Unicode by default, so they can represent characters from virtually any language.

What Is a String?

A string is an immutable sequence of characters. "Immutable" means you don't change a string in place; instead, operations create a new string.

123
title = "Python 101" emoji = "🙂" print(title, emoji)
copy

Creating Strings

Python lets you write string literals with single or double quotes; choose whichever makes the code clearer.

123456
a = "hello" b = 'he said: "hi"' c = "it's fine to mix quotes like this" print(a) print(b) print(c)
copy

If you need multiple lines, use triple-quoted strings. Python keeps the line breaks.

123
message = """Line 1 Line 2""" print(message)
copy

To include special characters (like a newline) in a single line, use escape sequences.

12
greet = "hello\nworld" # \n is a single newline character print(greet)
copy

When backslashes should be taken literally (e.g., Windows paths or simple regex patterns), a raw string helps.

12
path = r"C:\Users\alex\docs" # backslashes are not treated as escapes print(path)
copy

Inspecting Strings (without indexing yet)

You'll often need quick checks: "what is this?" and "how long is it?"

123
s = "hello" print(type(s)) # <class 'str'> print(len(s)) # 5
copy

An empty string is considered falsey, while any non-empty string is truthy. That's handy in conditions.

12345
name = "" if name: print("Welcome,", name) else: print("Please enter your name") # runs because "" is falsey
copy
Note
Note
  • "" (empty) is not the same as " " (a space). len("") == 0, len(" ") == 1;
  • \n counts as one character (a newline), so len("A\nB") == 3;
  • Prefer raw strings for paths to avoid accidental escapes like "\t" (tab).

1. Which is a valid string literal in Python?

2. What value will this code output?

3. Which statement is true?

question mark

Which is a valid string literal in Python?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

question mark

Which statement is true?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain more about string immutability?

What are some common string operations in Python?

How do I handle special characters in strings?

Awesome!

Completion rate improved to 5

bookCreating and Inspecting Strings

Scorri per mostrare il menu

Strings are how Python stores text - names, messages, file paths, even emoji. In Python 3, strings are Unicode by default, so they can represent characters from virtually any language.

What Is a String?

A string is an immutable sequence of characters. "Immutable" means you don't change a string in place; instead, operations create a new string.

123
title = "Python 101" emoji = "🙂" print(title, emoji)
copy

Creating Strings

Python lets you write string literals with single or double quotes; choose whichever makes the code clearer.

123456
a = "hello" b = 'he said: "hi"' c = "it's fine to mix quotes like this" print(a) print(b) print(c)
copy

If you need multiple lines, use triple-quoted strings. Python keeps the line breaks.

123
message = """Line 1 Line 2""" print(message)
copy

To include special characters (like a newline) in a single line, use escape sequences.

12
greet = "hello\nworld" # \n is a single newline character print(greet)
copy

When backslashes should be taken literally (e.g., Windows paths or simple regex patterns), a raw string helps.

12
path = r"C:\Users\alex\docs" # backslashes are not treated as escapes print(path)
copy

Inspecting Strings (without indexing yet)

You'll often need quick checks: "what is this?" and "how long is it?"

123
s = "hello" print(type(s)) # <class 'str'> print(len(s)) # 5
copy

An empty string is considered falsey, while any non-empty string is truthy. That's handy in conditions.

12345
name = "" if name: print("Welcome,", name) else: print("Please enter your name") # runs because "" is falsey
copy
Note
Note
  • "" (empty) is not the same as " " (a space). len("") == 0, len(" ") == 1;
  • \n counts as one character (a newline), so len("A\nB") == 3;
  • Prefer raw strings for paths to avoid accidental escapes like "\t" (tab).

1. Which is a valid string literal in Python?

2. What value will this code output?

3. Which statement is true?

question mark

Which is a valid string literal in Python?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

question mark

Which statement is true?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1
some-alt