Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Створення та Перевірка Рядків | Рядки
Типи Даних у Python

bookСтворення та Перевірка Рядків

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.

12345
# Displaying a course title with an icon course_title = "Data Types in Python" status_emoji = "🚀" print(course_title, status_emoji) # Output: Python for Beginners 🚀
copy

Creating Strings

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

12345678
# Showing how to handle quotes correctly in user messages greeting = "Hello!" message = 'User said: "Hi, nice to meet you!"' note = "It's fine to mix single and double quotes in text." print(greeting) print(message) print(note)
copy

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

123456
# Creating a multi-line email template email_template = """Dear User, Your subscription has been successfully renewed. Thank you for staying with us!""" print(email_template)
copy

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

123
# Formatting a welcome message for the user welcome_message = "Welcome to Codefinity!\nLet's start your learning journey." print(welcome_message)
copy

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

123
# Storing a file path on a Windows system file_path = r"C:\Users\alex\Documents\report.pdf" # backslashes are kept as-is print(file_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.

1234567
# Checking if the user entered their username during login username = "" if username: print(f"Welcome back, {username}!") else: print("Please enter your username") # runs because an empty string 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).
question mark

Which is a valid string literal in Python?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookСтворення та Перевірка Рядків

Свайпніть щоб показати меню

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.

12345
# Displaying a course title with an icon course_title = "Data Types in Python" status_emoji = "🚀" print(course_title, status_emoji) # Output: Python for Beginners 🚀
copy

Creating Strings

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

12345678
# Showing how to handle quotes correctly in user messages greeting = "Hello!" message = 'User said: "Hi, nice to meet you!"' note = "It's fine to mix single and double quotes in text." print(greeting) print(message) print(note)
copy

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

123456
# Creating a multi-line email template email_template = """Dear User, Your subscription has been successfully renewed. Thank you for staying with us!""" print(email_template)
copy

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

123
# Formatting a welcome message for the user welcome_message = "Welcome to Codefinity!\nLet's start your learning journey." print(welcome_message)
copy

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

123
# Storing a file path on a Windows system file_path = r"C:\Users\alex\Documents\report.pdf" # backslashes are kept as-is print(file_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.

1234567
# Checking if the user entered their username during login username = "" if username: print(f"Welcome back, {username}!") else: print("Please enter your username") # runs because an empty string 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).
question mark

Which is a valid string literal in Python?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1
some-alt