A tuple in Python is an ordered and immutable data structure used to store multiple values in a single variable. It is very similar to a list, but the main difference is that tuples cannot be changed after creation.
my_tuple = (1, 2, 3)
2. Creating a Tuple
Using parentheses:
fruits = ("apple", "banana", "cherry")
Parentheses are optional (a comma is enough):
colors = "red", "green", "blue"
For a single-element tuple, a trailing comma is required:
one_element = ("hello",)
3. Accessing Tuple Elements
You can access tuple elements using index numbers. Indexing starts at 0.
print(fruits[0]) # apple
print(fruits[-1]) # cherry
Bir metnin kaç karakterden oluştuğunu görmek için len() fonksiyonu kullanılır:
name = "Gökrem"
print(len(name)) # 6
6. Sık Kullanılan String Fonksiyonları
Python’da string’lerle çalışırken bazı yerleşik fonksiyonlar (metotlar) oldukça sık kullanılır:
.upper() → Tüm harfleri büyük harfe çevirir. Örnek: “gökrem”.upper() sonucu “GÖKREM” olur.
.lower() → Tüm harfleri küçük harfe çevirir. Örnek: “GÖKREM”.lower() sonucu “gökrem” olur.
.strip() → Metnin başındaki ve sonundaki boşlukları siler. Örnek: ” Hello “.strip() sonucu “Hello” olur.
.replace() → Metnin bir kısmını başka bir metinle değiştirir. Örnek: “Men came”.replace(“Men”, “Women”) sonucu “Women came” olur.
.split() → Belirtilen karaktere göre metni böler ve bir liste döndürür (varsayılan karakter boşluktur). Örnek: “John Wick”.split() sonucu [“John”, “Wick”]
Google Colab Gösterimi
"gökrem".upper() # GÖKREM
"GÖKREM".lower() # gökrem
" Hello ".strip() # Hello
"Men came".replace("Men", "Women") # Women came
"John Wick".split() # ['John', 'Wick'] → bir liste örneği