Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Understanding String Basics | String Manipulation Essentials
Working with Strings and Data Formats

bookUnderstanding String Basics

Strings are sequences of characters used to represent text in Python. You define a string by enclosing characters in single quotes ('Hello') or double quotes ("Hello"). Strings in Python are immutable, which means that once you create a string, you cannot change its contents. Any operation that seems to modify a string actually creates a new string object in memory. This immutability ensures that strings remain consistent and safe to use throughout your code. When you assign a string to a variable, Python stores a reference to the string object in memory, not the actual characters themselves.

123456789101112131415161718192021222324252627
# String indexing and slicing examples text = "Python" # Positive indexing: first character is at index 0 first_char = text[0] # 'P' third_char = text[2] # 't' # Negative indexing: last character is at index -1 last_char = text[-1] # 'n' second_last_char = text[-2] # 'o' # Slicing: extracting substrings first_three = text[0:3] # 'Pyt' middle_chars = text[2:5] # 'tho' from_start = text[:4] # 'Pyth' to_end = text[3:] # 'hon' all_but_last = text[:-1] # 'Pytho' print("First character:", first_char) print("Third character:", third_char) print("Last character:", last_char) print("First three characters:", first_three) print("Characters 2 to 4:", middle_chars) print("From start to index 3:", from_start) print("From index 3 to end:", to_end) print("All but last character:", all_but_last)
copy

You can combine strings or repeat them using special operators. The + operator concatenates two strings, joining them together into a new string. The * operator repeats a string a specified number of times, creating a new string with the original repeated. For example, "Hello" + "World" produces "HelloWorld", and "Hi" * 3 results in "HiHiHi". These operations do not modify the original strings but instead return new string objects.

1. Which operator is used to concatenate two strings in Python?

2. Select all valid ways to slice a string in Python.

question mark

Which operator is used to concatenate two strings in Python?

Select the correct answer

question mark

Select all valid ways to slice a string in Python.

Select the correct answer

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

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

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

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

Запитати АІ

expand

Запитати АІ

ChatGPT

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

Suggested prompts:

Can you explain more about string immutability in Python?

What happens if I try to change a character in a string?

Can you show more examples of string slicing or concatenation?

Awesome!

Completion rate improved to 6.67

bookUnderstanding String Basics

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

Strings are sequences of characters used to represent text in Python. You define a string by enclosing characters in single quotes ('Hello') or double quotes ("Hello"). Strings in Python are immutable, which means that once you create a string, you cannot change its contents. Any operation that seems to modify a string actually creates a new string object in memory. This immutability ensures that strings remain consistent and safe to use throughout your code. When you assign a string to a variable, Python stores a reference to the string object in memory, not the actual characters themselves.

123456789101112131415161718192021222324252627
# String indexing and slicing examples text = "Python" # Positive indexing: first character is at index 0 first_char = text[0] # 'P' third_char = text[2] # 't' # Negative indexing: last character is at index -1 last_char = text[-1] # 'n' second_last_char = text[-2] # 'o' # Slicing: extracting substrings first_three = text[0:3] # 'Pyt' middle_chars = text[2:5] # 'tho' from_start = text[:4] # 'Pyth' to_end = text[3:] # 'hon' all_but_last = text[:-1] # 'Pytho' print("First character:", first_char) print("Third character:", third_char) print("Last character:", last_char) print("First three characters:", first_three) print("Characters 2 to 4:", middle_chars) print("From start to index 3:", from_start) print("From index 3 to end:", to_end) print("All but last character:", all_but_last)
copy

You can combine strings or repeat them using special operators. The + operator concatenates two strings, joining them together into a new string. The * operator repeats a string a specified number of times, creating a new string with the original repeated. For example, "Hello" + "World" produces "HelloWorld", and "Hi" * 3 results in "HiHiHi". These operations do not modify the original strings but instead return new string objects.

1. Which operator is used to concatenate two strings in Python?

2. Select all valid ways to slice a string in Python.

question mark

Which operator is used to concatenate two strings in Python?

Select the correct answer

question mark

Select all valid ways to slice a string in Python.

Select the correct answer

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

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

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

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