Pythongstringfuncs
Pythongstringfuncs
----------------
Insert the price inside the placeholder, the price should be in fixed point, two-
decimal format:
The Placeholders
----------------
The placeholders can be identified using named indexes {price}, numbered indexes
{0}, or even empty placeholders {}.
Example
Using different placeholder values:
index() Method
--------------
Where in the text is the word "welcome"?:
Syntax
string.index(value, start, end)
Parameter Description
value Required. The value to search for
start Optional. Where to start the search. Default is 0
end Optional. Where to end the search. Default is to the end of
the string
Example
Where in the text is the first occurrence of the letter "e"?:
Example
Where in the text is the first occurrence of the letter "e" when you only search
between position 5 and 10?:
Example
If the value is not found, the find() method returns -1, but the index() method
will raise an exception:
isalnum() Method
txt = "Company12"
x = txt.isalnum()
print(x)
txt = "CompanyX"
x = txt.isalpha()
print(x)
The isalpha() method returns True if all the characters are alphabet letters (a-z).
isascii() Method
txt = "Company123"
x = txt.isascii()
print(x)
The isascii() method returns True if all the characters are ascii characters (a-
z).
isdecimal() Method
txt = "1234"
x = txt.isdecimal()
print(x)
isdigit() Method
txt = "50800"
x = txt.isdigit()
print(x)
The isdigit() method returns True if all the characters are digits, otherwise
False.
Exponents, like ², are also considered to be a digit.
islower() Method
a = "Hello world!"
b = "hello 123"
c = "mynameisPeter"
print(a.islower())
print(b.islower())
print(c.islower())
isspace() Method
The isspace() method returns True if all the characters in a string are
whitespaces, otherwise False.
x = txt.isspace()
print(x)
isupper() Method
x = txt.isupper()
print(x)
join() Method
Join all items in a tuple into a string, using a hash character as separator:
Join all items in a dictionary into a string, using the word "TEST" as separator:
ljust() Method
The ljust() method will left align the string, using a specified character (space
is default) as the fill character.
txt = "banana"
x = txt.ljust(20, "O")
print(x)
lower() Method
The lower() method returns a string where all characters are lower case.
x = txt.lower()
print(x)
partition() Method
x = txt.partition("bananas")
print(x)
zfill() Method
The zfill() method adds zeros (0) at the beginning of the string, until it reaches
the specified length.
If the value of the len parameter is less than the length of the string, no filling
is done.
Fill the strings with zeros until they are 10 characters long:
a = "hello"
b = "welcome to the jungle"
c = "10.000"
print(a.zfill(10))
print(b.zfill(10))
print(c.zfill(10))
The upper() method returns a string where all characters are in upper case.
x = txt.upper()
print(x)
swapcase() Method
The swapcase() method returns a string where all the upper case letters are lower
case and vice versa.
x = txt.swapcase()
print(x)
strip() Method
Remove spaces at the beginning and at the end of the string:
x = txt.strip()
startswith() Method
x = txt.startswith("Hello")
print(x)
splitlines() Method
The splitlines() method splits a string into a list. The splitting is done at line
breaks.
x = txt.splitlines(True)
print(x)
rsplit() Method
Split a string into a list, using comma, followed by a space (, ) as the separator:
x = txt.rsplit(", ")
print(x)
The rsplit() method splits a string into a list, starting from the right.
print(x)