Python G String Fun Cs
Python G String Fun Cs
----------------------
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"?:
txt = "Hello, welcome to my world."
x = txt.index("e")
print(x)
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:
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
---------------------
Check if all the characters in the text are ascii characters:
txt = "Company123"
x = txt.isascii()
print(x)
The isascii() method returns True if all the characters are ascii characters (a-
z).
isdecimal() Method
------------------------
Check if all the characters in a string are decimals (0-9):
txt = "1234"
x = txt.isdecimal()
print(x)
isdigit() Method
---------------------
Check if all the characters in the text are digits:
txt = "50800"
x = txt.isdigit()
print(x)
The isdigit() method returns True if all the characters are digits, otherwise
False.
islower() Method
----------------------
Check if all the characters in the text are in lower case:
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.
isupper() Method
-----------------------
Check if all the characters in the text are in upper case:
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.
Symbols and Numbers are ignored.
partition() Method
------------------------
Search for the word "bananas", and return a tuple with three elements:
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))
upper()
---------
The upper() method returns a string where all characters are in upper case.
swapcase() Method
-------------------------
The swapcase() method returns a string where all the upper case letters are lower
case and vice versa.
strip() Method
--------------------
Remove spaces at the beginning and at the end of the string:
startswith() Method
--------------------------
Check if the string starts with "Hello":
splitlines() Method
-------------------------
The splitlines() method splits a string into a list. The splitting is done at line
breaks.
rsplit() Method
-------------------
Split a string into a list, using comma, followed by a space (, ) as the separator:
The rsplit() method splits a string into a list, starting from the right.