🔪 Slicing Strings in Python
Slicing a string is just like taking a slice out of a homemade apple pie. When it comes to strings, slices can be as small or as big as you want. If you want to put two or more of those slices back together, you join them together end-to-end—in Python speak, you “concatenate” them—to make one bigger, single string.
      🔪 Slicing Strings in Python
      
    
    
    
  
  Slicing means extracting parts of a string using indexes.
🧠 Syntax:
1
string[start:stop:step]
| Part | Meaning | 
|---|---|
start | 
      Index to begin the slice (inclusive) | 
stop | 
      Index to end the slice (exclusive) | 
step | 
      How many characters to skip (default: 1) | 
🧪 Example:
1
2
3
4
5
6
7
text = "Python"
print(text[0:2])    # 'Py' → from index 0 up to (but not including) 2
print(text[2:])     # 'thon' → from index 2 to the end
print(text[:3])     # 'Pyt' → from start to index 3
print(text[::2])    # 'Pto' → every 2nd character
print(text[::-1])   # 'nohtyP' → reversed string
🔗 Joining Strings in Python
Joining means combining a list (or any iterable) of strings into one string.
🧠 Syntax:
1
'separator'.join(iterable)
'separator'is what you want between each piece (like a space, comma, dash, etc.)iterableis usually a list of strings
🧪 Example:
1
2
3
4
5
6
7
words = ["Python", "is", "fun"]
sentence = " ".join(words)
print(sentence)  # 'Python is fun'
csv_line = ",".join(words)
print(csv_line)  # 'Python,is,fun'
🔁 Slicing + Joining Example
1
2
3
4
5
6
7
8
word = "developer"
# Slice out every other letter
sliced = word[::2]  # 'deelpr'
# Join with dashes
joined = "-".join(sliced)
print(joined)  # 'd-e-e-l-p-r'
🧠 Summary Table
| Operation | Syntax Example | Result | 
|---|---|---|
| Slice | word[1:4] | 
      characters from 1 to 3 | 
| Reverse | word[::-1] | 
      reverses the string | 
| Join (space) | " ".join(["a", "b"]) | 
      'a b' | 
    
| Join (dash) | "-".join(["a", "b"]) | 
      'a-b' | 
    
        
          
          This post is licensed under 
        
          CC BY 4.0
        
         by the author.