๐ฏ What You'll Master
- Basic variable interpolation with {variable}
- Expression evaluation inside strings
- Array and object access in interpolation
- Function calls within strings
- Advanced interpolation patterns and best practices
๐ค Basic Interpolation
The foundation of B3D's string interpolation is the {variable} syntax.
<Data>
name = 'Berke Oruรง'
language = 'B3D'
version = '1.0.0'
</Data>
<Main>
// Simple variable interpolation
print('Hello, I am {name}')
print('Welcome to {language} v{version}')
// Multiple variables in one string
print('{language} v{version} by {name} is amazing!')
</Main>
๐งฎ Expression Interpolation
B3D can evaluate mathematical expressions and function calls inside interpolation.
<Data>
age = 25
price = 99.99
quantity = 3
</Data>
<Main>
// Mathematical expressions
print('Next year I will be {age + 1} years old')
print('Total cost: ${price * quantity}')
// Complex calculations
print('Average age in 10 years: {(age + 10 + age + 8) / 2}')
// Using parentheses for clarity
discount = 0.1
print('After 10% discount: ${price * (1 - discount)}')
</Main>
๐ Array and Object Access
Access array elements and object properties directly in interpolation.
<Data>
colors = ['red', 'green', 'blue']
scores = [95, 87, 92]
person = {
'name': 'Alice',
'age': 30,
'city': 'New York'
}
</Data>
<Main>
// Array access
print('First color: {colors[0]}')
print('Last score: {scores[-1]}')
// Object property access
print('{person["name"]} is {person["age"]} years old')
print('She lives in {person["city"]}')
// Combining array and object access
print('Favorite color: {colors[0]}, Best score: {scores[0]}')
</Main>
๐ง Function Calls in Interpolation
Call built-in functions directly inside string interpolation.
<Data>
numbers = [10, 20, 30, 40, 50]
text = ' Hello, B3D World! '
</Data>
<Main>
// Built-in math functions
print('Square root of 16: {math_sqrt(16)}')
print('2 to the power of 8: {math_pow(2, 8)}')
// Data analysis functions
print('Average: {data_mean(numbers)}')
print('Sum: {data_sum(numbers)}')
// String processing functions
print('Cleaned text: "{str_trim(text)}"')
print('Length: {str_length(str_trim(text))} characters')
</Main>
๐ก Pro Tips
- Escape braces: Use
\{and\}to include literal braces - Complex expressions: Use parentheses for clarity in complex calculations
- Function chaining: You can chain function calls like
{str_upper(str_trim(text))} - Performance: Simple variable access is faster than complex expressions
๐ฅ Advanced Examples
Real-world examples combining multiple interpolation techniques.
<Data>
students = [
{'name': 'Alice', 'scores': [95, 87, 92]},
{'name': 'Bob', 'scores': [88, 91, 85]}
]
course_name = 'B3D Programming'
</Data>
<Main>
// Complex nested access
print('Course: {course_name}')
print('Student: {students[0]["name"]}')
print('First score: {students[0]["scores"][0]}')
// Combining functions and expressions
alice_avg = data_mean(students[0]["scores"])
print('{students[0]["name"]} average: {alice_avg}')
// Advanced string formatting
print('Report: {str_upper(students[0]["name"])} scored {math_round(alice_avg)}% in {course_name}')
</Main>
๐ฏ Practice Exercises
Exercise 1: Personal Dashboard
Create a personal dashboard that displays your information using interpolation.
Exercise 2: Math Calculator
Build a calculator that shows mathematical operations using expression interpolation.
Exercise 3: Data Report Generator
Create a report generator that processes arrays and objects with function calls in interpolation.
๐ Congratulations!
You've mastered B3D's string interpolation! You can now:
- โ Use basic variable interpolation
- โ Evaluate expressions in strings
- โ Access arrays and objects in interpolation
- โ Call functions within string templates
- โ Create complex, dynamic strings