To get string length (number of characters), we will use len()
function.
Note: all whitespaces are important:
a = " Double Quotes"
b = 'Single Quotes '
print(len(a))
print(len(b))
Result:
19
22
>>>
a = "Double Quotes"
b = 'Single Quotes'
print(len(a))
print(len(b))
Result:
13
13
>>>
Sometimes it's useful to put length in dedicated variable, so we can use it later:
a = "Double Quotes"
b = 'Single Quotes'
how_a_is_long = len(a)
print(how_a_is_long)
Result:
13
>>>
Even better option is to explain what is happening:
a = "Double Quotes"
b = 'Single Quotes'
how_a_is_long = len(a)
print("How long var a is: ", how_a_is_long)
To transfer all chars to lowercases, we can use lower()
function:
a = "Double Quotes"
b = 'Single Quotes'
print(a.lower())
print(b.lower())
Result:
double quotes
single quotes
>>>
Same for uppercases, with upper()
function:
a = "Double Quotes"
b = 'Single Quotes'
print(a.upper())
print(b.upper())
Result:
DOUBLE QUOTES
SINGLE QUOTES
>>>
No comments:
Post a Comment