
In Python:
s = "hello"
s is a str object (an instance of the built-in str class).s.upper(), s.replace(), s.find(), etc.| Feature | Python str | C/C++ char[] / Java char[] |
|---|---|---|
| Type | str object | Raw character array |
| Mutable? | ❌ Immutable | ✅ Mutable in C/C++, sometimes in Java |
| Unicode support | ✅ Built-in | ❌ Needs special handling |
| Built-in methods | ✅ Tons of methods (.upper()) | ❌ Manual function writing needed |
| Indexable | ✅ Yes | ✅ Yes |
s = "cat"
print(s[1]) # 'a'
This doesn't:
s[1] = 'o' # ❌ TypeError: 'str' object does not support item assignment
If you want to modify a string, you have to create a new one:
s = "cat"
s = s[:1] + 'o' + s[2:]
print(s) # 'cot'
An f-string (short for "formatted string literal") is a way to embed expressions inside string literals using curly braces {}.
name = "Nara"
age = 25
greeting = f"Hello, my name is {name} and I’m {age} years old."
print(greeting)
# Output: Hello, my name is Nara and I’m 25 years old.
f before the string tells Python to evaluate any expressions inside {} and insert the result directly into the string.f"The sum of 2 and 3 is {2 + 3}" # "The sum of 2 and 3 is 5"
| Feature | Regular String | f-String |
|---|---|---|
| Syntax | "Hello" | f"Hello {name}" |
| Expression Evaluation | Not supported | Supported inside {} |
| Convenience | Manual string formatting needed | Very concise & readable |
# Regular string with .format()
name = "Nara"
msg = "Hello, {}!".format(name)
# f-string version
msg = f"Hello, {name}!" # cleaner and more readable
Perfect! Now that you’ve got the concept of Python strings as full-blown objects, let’s look under the hood at how f-strings work.
When you write:
name = "Nara"
msg = f"Hello, {name.upper()}!"
Python evaluates everything inside the {} at runtime and converts it to a string using str() (unless you override it).
msg = "Hello, " + str(name.upper()) + "!"
So, f-strings:
str() on results by default (you can override that with format specs)Let’s take a peek using the dis module (Python's disassembler):
import dis
def greet(name):
return f"Hello, {name}!"
dis.dis(greet)
Output (simplified):
3 0 LOAD_CONST 1 ('Hello, ')
2 LOAD_FAST 0 (name)
4 FORMAT_VALUE 0
6 BUILD_STRING 2
8 RETURN_VALUE
LOAD_CONST 'Hello, ' → loads the literal partLOAD_FAST name → gets the variableFORMAT_VALUE → evaluates and formats the expressionBUILD_STRING → joins the parts togetherSo under the hood, f-strings are compiled into bytecode instructions that:
1. Evaluate expressions in {}
2. Convert them with str() (or repr() or format(), if you specify)
3. Concatenate them with the literal parts
Here’s how different methods behave:
name = "Nara"
age = 25
| Method | Code | Internally does |
|---|---|---|
| f-string | f"{name} is {age}" | Compile-time template + str() |
.format() | "{} is {}".format(name, age) | Run-time lookup and string building |
% formatting | "%s is %d" % (name, age) | Old-school C-style formatting |
f-strings are faster because:
pi = 3.14159
f"Pi is {pi:.2f}" # Pi is 3.14
f"{name:>10}" # Right-align name in a field of 10 chars
.2f, >10, etc.__format__() method of the object.f"..." lets you embed variables/expressions in strings."{}".format(...) or string concatenation.{} and apply str()..format() or %.FORMAT_VALUE and BUILD_STRING.본 후기는 [한글과컴퓨터x한국생산성본부x스나이퍼팩토리] 한컴 AI 아카데미 (B-log) 리뷰로 작성 되었습니다.
#한컴AI아카데미 #AI개발자 #AI개발자교육 #한글과컴퓨터 #한국생산성본부 #스나이퍼팩토리 #부트캠프 #AI전문가양성 #개발자교육 #개발자취업
i don't know english