Course Ten - String & variables in python
By ienex
Strings are one of the most commonly used data types in Python. They are easily created by enclosing characters in single ('
) or double ("
) quotes.
var1 = 'Hello World!'
var2 = "Python Programming"
Accessing Values in Strings
In Python, characters are treated as strings of length one, so a single character is considered a substring. You can access substrings using square brackets []
:
var1 = 'Hello World!'
var2 = "Python Programming"
print("var1[0]:", var1[0])
print("var2[1:5]:", var2[1:5])
Output:
var1[0]: H
var2[1:5]: ytho
Updating Strings
Strings are immutable, so to update a string, you must assign a new value to the variable. You can also concatenate parts of the old string with a new string:
var1 = 'Hello World!'
print("Updated String:", var1[:6] + 'Python')
Output:
Updated String: Hello Python
Escape Characters
Escape characters allow you to include non-printable characters or characters with special meaning in strings.
Escape | Hex | Description |
---|---|---|
\a |
0x07 | Bell / Alert |
\b |
0x08 | Backspace |
\cx |
- | Control-x |
\e |
0x1b | Escape |
\f |
0x0c | Form feed |
\n |
0x0a | Newline |
\r |
0x0d | Carriage return |
\s |
0x20 | Space |
\t |
0x09 | Tab |
\v |
0x0b | Vertical tab |
\xhh |
- | Hexadecimal character |
String Operators
Assume:
a = 'Hello'
b = 'Python'
Operator | Description | Example |
---|---|---|
+ |
Concatenate strings | a + b → HelloPython |
* |
Repeat string | a * 2 → HelloHello |
[] |
Access character | a[1] → e |
[:] |
Slice substring | a[1:4] → ell |
in |
Membership test | 'H' in a → True |
not in |
Not in string | 'M' not in a → True |
r/R |
Raw string | r'\n' → prints \n |
% |
String formatting | See below |
String Formatting with %
Python supports C-style string formatting using %
:
print("My name is %s and weight is %d kg!" % ('Zara', 21))
Output:
My name is Zara and weight is 21 kg!
Format Specifiers:
Symbol | Description |
---|---|
%c |
Character |
%s |
String |
%d |
Integer |
%i |
Integer |
%u |
Unsigned integer |
%o |
Octal |
%x |
Hexadecimal (lowercase) |
%X |
Hexadecimal (uppercase) |
%e |
Exponential (lowercase e) |
%E |
Exponential (uppercase E) |
%f |
Floating-point |
%g |
Shorter of %f or %e |
%G |
Shorter of %f or %E |
Additional formatting options:
Symbol | Function |
---|---|
* |
Minimum width / precision |
- |
Left align |
+ |
Show sign |
<space> |
Leading space for positive numbers |
# |
Add 0 , 0x , or 0X for octal/hex |
0 |
Pad with zeros |
%% |
Print % character |
Triple-Quoted Strings
Triple quotes allow multi-line strings including special characters like newlines \n
or tabs \t
:
para_str = """This is a long string
that spans multiple lines
and includes special characters like TAB (\t)
and newline (\n) characters."""
print(para_str)
Output:
This is a long string
that spans multiple lines
and includes special characters like TAB ( )
and newline
characters.
Raw Strings
Raw strings ignore escape sequences, so backslashes are printed as-is:
print(r'C:\now')
Output:
C:\now
This version is fully formatted for Blogger, including code blocks, tables, and examples.
If you want, I can combine all your Python tutorial posts into a single, clean Blogger-ready series with proper headings and links for easier reading.
Do you want me to do that?
Comments
Post a Comment