A06 Strings

String Length

# Calculate the length of a string
# ------------------------------------------------------------------------------
# This snippet calculates the number of characters in a string. The
# result differs from the byte length when the string contains UTF-8
# characters that use multiple bytes.

text = "0123456789"
print(len(text))
# Output:
# 10

text = 'Здравей!'
print(len(text))
# Output:
# 8

String Concatenation

# String concatenation example
# ------------------------------------------------------------------------------
# This code concatenates (joins) strings using the `+` operator. This
# technique is common when constructing messages or combining text from
# different sources.


string1 = 'Hello'
string2 = "World"
string3 = "!"

result = string1 + " " + string2 + string3
print(result)
# Output:
# Hello World!

String Interpolation

# String interpolation
# ------------------------------------------------------------------------------
# This code uses f-strings (formatted string literals) to create strings
# that include variables. Expressions inside curly braces `{}` are
# evaluated in place.

first_name = "Branimir"
last_name = "Georgiev"
age = 25

print(f"My name is {first_name} {last_name} and I am {age} years old")
# Output:
# My name is Branimir Georgiev and I am 25 years old

String Escaping

# Escape sequences in Python strings
# ------------------------------------------------------------------------------
# This example uses escape sequences in strings so you can include
# characters that would otherwise be difficult or impossible to type
# directly.
#
# Special characters:
# - `\n` for new line
# - `\t` for tab
# - `\\` for backslash
# - `\'` for single quote
# - `\"` for double quote

print("Hello\nWorld")           # Print new line inside string
print("He said, \"Goodbye\"")   # Print double quotes inside string
# Output:
# Hello
# World
# He said, "Goodbye"

String Indexing

# String indexing
# ------------------------------------------------------------------------------
# This code accesses individual characters in a string by index. Strings are
# sequences of characters, so index 0 refers to the first character, and
# negative indices let you read characters from the end of the string.

string = "Hello, world!"

print(string[0])    # Print first character
# Output:
# H

print(string[1])    # Print second character
# Output:
# e

print(string[-1])   # Print last character
# Output:
# !

String Slicing

# String slicing
# ------------------------------------------------------------------------------
# This code slices strings in Python. Slicing extracts a portion of a string
# by specifying a start index, an end index, and an optional step using
# the syntax `string[start:end:step]`.
#
# The `start` index is inclusive, the `end` index is exclusive, and the `step`
# determines the increment between each index in the slice. The default values
# for `start` is 0, for `end` is the length of the string, and for `step` is 1.

text = "0123456789ABCDEF"

print(text[0:5])
# Output: 01234

print(text[7:])
# Output: 789ABCDEF

print(text[:5])
# Output: 01234

print(text[::2])
# Output: 02468ACE

print(text[::-1])
# Output: FEDCBA9876543210

print(text[1:10:2])
# Output: 13579

String Splitting

# Splitting Strings
# ------------------------------------------------------------------------------
# The `split()` method is used to split a string into a list of tokens based
# on a specified separator. If no separator is provided, it defaults to
# whitespace. The `splitlines()` method is used to split a string into a
# list of lines.


text = "1 2 3 4 5 6 7 8 9 10"
print(text.split())
# Output:
# ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']


text = "1, 2, 3 4 5 6 7 8 9 10"
print(text.split(', '))
# Output:
# ['1', '2', '3 4 5 6 7 8 9 10']

text = """ First line
Second line
Third line
"""
print(text.splitlines())
# Output:
# [' First line', 'Second line', 'Third line']

String Joining

# String joining
# ------------------------------------------------------------------------------
# The `join()` method is a string method that takes an iterable (like a list)
# and concatenates its elements into a single string, with a specified separator
# between each element. If no separator is provided, it defaults to an empty
# string, effectively concatenating the elements without any characters in
# between.

tokens = '1 2 3 4 5 6 7 8 9'.split()
print(tokens)
# Output:
# ['1', '2', '3', '4', '5', '6', '7', '8', '9']

text = ''.join(tokens)
print(text)
# Output:
# 123456789

text = ' '.join(tokens)
print(text)
# Output:
# 1 2 3 4 5 6 7 8 9

text = ','.join(tokens)
print(text)
# Output:
# 1,2,3,4,5,6,7,8,9

String Case

# String case manipulation examples
# ------------------------------------------------------------------------------
# This code applies various string case manipulation methods in Python.

# Store the string to be manipulated
text = "AAAA bbbb"

print(text.upper())
# Output:
# AAAA BBBB

print(text.lower())
# Output:
# aaaa bbbb

print(text.title())
# Output:
# Aaaa Bbbb

print(text.capitalize())
# Output:
# Aaaa bbbb

print(text.swapcase())
# Output:
# aaaa BBBB

String Encoding

# String encoding and decoding