GitHub

Strings

Stable String operations are stable in Levython 1.0.

Strings in Levython are immutable sequences of characters. They are interned for efficient storage and comparison, with all string literals sharing the same memory location if they have identical content.

Creating Strings#

String literals are enclosed in double quotes:

levython
# String literals
greeting <- "Hello, World!"
name <- "Levython"
empty <- ""

# String length
len_greeting <- len(greeting)
say("Length: " + str(len_greeting))  # Length: 13

# Access characters (strings are sequences)
first_char <- greeting[0]  # "H"

Concatenation#

Use the + operator to concatenate strings:

levython
first <- "Hello"
last <- "World"

# Concatenation
full <- first + ", " + last + "!"
say(full)  # Hello, World!

# Building strings in loops
result <- ""
for i in range(1, 4) {
    result <- result + str(i) + " "
}
say(result)  # "1 2 3 "
Performance Note: String concatenation creates new string objects. For building long strings in loops, consider using a list and join() for better performance.

String Functions#

Function Description Example
len(string) Get string length len("hello") โ†’ 5
upper(string) Convert to uppercase upper("hello") โ†’ "HELLO"
lower(string) Convert to lowercase lower("HELLO") โ†’ "hello"
trim(string) Remove whitespace trim(" hi ") โ†’ "hi"
replace(str, old, new) Replace substring replace("hi hi", "hi", "bye") โ†’ "bye bye"
split(string, delim) Split into list split("a,b,c", ",") โ†’ ["a", "b", "c"]
join(list, delim) Join list to string join(["a", "b"], ",") โ†’ "a,b"

Searching#

levython
text <- "Hello, World!"

# Check if substring exists
has_world <- contains(text, "World")
say("Contains 'World': " + str(has_world))  # true

# Find position
pos <- find(text, "World")
say("Position of 'World': " + str(pos))  # 7

# Check prefix/suffix
starts <- startswith(text, "Hello")
ends <- endswith(text, "!")
say("Starts with 'Hello': " + str(starts))  # true
say("Ends with '!': " + str(ends))          # true

Case Conversion#

levython
text <- "Hello, World!"

# Convert to uppercase
upper_text <- upper(text)
say(upper_text)  # HELLO, WORLD!

# Convert to lowercase
lower_text <- lower(text)
say(lower_text)  # hello, world!

Splitting & Joining#

levython
# Split string into list
csv <- "apple,banana,cherry"
fruits <- split(csv, ",")
say(str(fruits))  # ["apple", "banana", "cherry"]

# Join list into string
words <- ["Hello", "from", "Levython"]
sentence <- join(words, " ")
say(sentence)  # Hello from Levython

# Efficient string building
parts <- []
for i in range(1, 6) {
    append(parts, str(i))
}
result <- join(parts, ", ")
say(result)  # 1, 2, 3, 4, 5

Common Patterns

levython
# Pattern: Clean user input
user_input <- "  hello@example.com  "
email <- trim(lower(user_input))
say(email)  # hello@example.com

# Pattern: Replace multiple occurrences
text <- "The cat in the hat"
new_text <- replace(text, "the", "a")
say(new_text)  # a cat in a hat

# Pattern: Parse CSV-like data
data <- "John,25,Engineer"
fields <- split(data, ",")
name <- fields[0]
age <- int(fields[1])
job <- fields[2]
say(name + " is " + str(age) + " and works as a " + job)
String Interning: Levython automatically interns string literals, meaning identical strings share the same memory. This makes string comparison extremely fast (pointer comparison) and reduces memory usage.