In python the strings are enclose with '' or "". Its really important to notice that a string is an immutable data type, so if you want to edit a string, you will need to create a new one or rewrite the same variable, so keep in mind that to modify a string in any way (capitalize, add one letter, remove the last element), has a complexity of O(N).
A = ""
B = "Hello"
C = 'World!'
D = A + B + ' ' + C # D = "Hello World!"
E = 3*B # E = "HelloHelloHello"
F = str(10) # F = "10"
B[0] # "H"
B[:2] # "HO"
B[-1] # "A"
B[-3:] # "OLA"
B[1:3] # "OL"
# Some functions applied to the strings
type(A) # Returns < class 'str' >
len(A) # The length of A is 0, as it has 0 element
len(B) # The length of B is 5, as it has 5 elements
'll' in B # True
del A # deletes the string A
Do you remember how to write a comment? Well, a comment is actually a multiline string in Python, something cool to keep in mind.
""" This is a single comment"""
A = """This is a multiline string,
that can be read as a comment as well.
Funfact
"""
A # A = "This is a multiline string, \nthat can be read as a comment as well\nFunfact"
Method | Description |
---|---|
capitalize() | Returns a string where the first character changed to upper case |
upper() | Returns the string converted into upper case |
lower() | Returns the string converted into lower case |
swapcase() | Returns the Swaps cases, lower case becomes upper case and vice versa |
strip() | Returns a version of the string (without spaces at the beggining and the end) |
count(x) | Returns how many elements inside of the string are the same than "x" |
find(x) | Returns the first index where "x" appears. It returns -1 if "x" is not inside of the string |
index(x) | Returns the first index where "x" appears. It gives an error if "x" is not inside of the string |
replace(x,y) | Returns a string where a "x is replaced with "y" |
isalnum() | Returns True if all elements in the string are alphanumeric |
isalpha() | Returns True if all elements in the string are in the alphabet |
isdecimal() | Returns True if all elements in the string are decimals (0-9) |
isupper() | Returns True if all elements in the string are upper case |
islower() | Returns True if all elements in the string are lower case |
split(x) | Splits the string at "x", and returns a list |
join(X) | Takes all items in "X" and joins them into one string with the main string as separator. |
format(x) | Returns a modified version of the string where inserts varibles (x) inside the string's placeholder. "{}" |
A = "hello world!"
B = A.capitalize() # B = "Hello world!"
C = A.upper() # C = "HELLO WORLD!"
D = C.lower() # D = "hello world!"
E = B.swapcase() # E = "hELLO WORLD!"
F = " Hello World! ".strip() # F = "Hello world!"
G = A.count('l') # G = 3
H = A.count('ll') # H = 1
I = A.find('?') # I = -1, not found
J = A.index('o') # J = 4
K = B.replace('w','W') # K = "Hello World!"
L = K.replace('!','') # L = "Hello World"
M = "5 is a number!".isalnum() # M = False
N = "5isanumber!".isalnum() # N = True
O = "Word!".isalpha() # O = False
P = "word".isalpha() # P = True
Q = "2.3".isdecimal() # Q = False
R = "23".isdecimal() # R = True
S = "Word".isupper() # S = False
T = "WORD".isupper() # T = True
U = "Word".islower() # U = False
V = "word".islower() # V = True
W = A.split(' ') # W = ["hello", "world!"]
X = '# &'.join(['A','B','C']) # X = "A# &B# &C"
Y = 'My first name is {}, and my last name is {}'.format('Albert', 'Einstein')
Z = "Today is {m} {d}{e}, {y}.".format(d=3, e='rd', m='February', y=2021)
String | Description |
---|---|
\\ | \ |
\' | ' |
\" | " |
\b | Backspace |
\t | Tap space |
\n | New line |
A = "\\ this is a single Backslash" # A = \ this is a single Backslash
B = 'I am \'Coding\' ' # B = "I am 'coding' "
C = "I am \"Coding\" " # C = 'I am "coding" '
D = 'Hello \bWorld!' # D = 'HelloWorld!'
E = 'Hello\tWorld!' # E = 'Hello World!'
F = 'Hello\nWorld!'
"""
F = 'Hello
World!'
"""