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
Strings and Data Formats in Python

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?

正しい答えを選んでください

question mark

Select all valid ways to slice a string in Python.

すべての正しい答えを選択

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  1

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  1
some-alt