Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PYTHON

STRINGS

STRING INDEXING: TO ACCESS A PARTICULAR CHARACTER FROM A STRING


text == “Python”
print(text[0])
P
SIMILARLY A STRINGS LOCATION CAN ALSO BE DERIVED VICE VERSA.
text == “Python”
text.index(“y”)
1

INDEX ERROR: TRYING TO INDEX LARGER THAN STRING

SLICE / SUBSTRING: ACCESS A PORTION OF A STRING


fruit = "Mangosteen"
fruit[1:4]
'Ang'

UPPER CASE & LOWER CASE:

“Mountains”.upper()
‘MOUNTAINS’
“Mountains”.lower()
‘Mountains’

STRIP: GETS RID OF SURROUNDING SPACES

“ yes “.strip()
‘yes’
“ yes “.lstrip()
‘yes ’
“ yes “.rstrip()
‘ yes’

COUNT:

“The number of times e occurs in this string is 4”.count(“e”)


4

ENDSWITH: CONFIRMS WHETHER STRING ENDS WITH A SPECIFIC SUBSTRING.

“Forest”.endswith(“rest”)
True
ISNUMERIC:

“Forest”.isnumeric()
False
“1234”.isnumeric()
True

CONVERT STRING INTO INTEGER:

int(“1000”) + int(“1000”)
2000

USING JOINT METHOD FOR CONCATENATION:

“ “.join([“This”, “is”, “a”, “phrase”])


‘This is a phrase’
“...“.join([“This”, “is”, “a”, “phrase”])
‘This…is…a…phrase’

SPLITTING A STRING INTO LIST:

“This is another example”.split()


[‘This’, ‘is’, ‘another’, ‘example’]

Fill in the gaps in the initials function so that it returns the initials of the words contained in
the phrase received, in upper case. For example: "Universal Serial Bus" should return "USB";
"local area network" should return "LAN”.

def initials(phrase):

words = phrase.split()

result = ""

for word in words:

result += word[0].upper()

return result

int(initials("Universal Serial Bus")) # Should be: USB

int(initials("local area network")) # Should be: LAN

int(initials("Operating system")) # Should be: OS


STRING FORMATTING:

name = “Manny”
number = “20”
print(“{} you chose {}”.format(name, number))

USING FLOAT ADDITION & INPUT

USING CHARACTER LIMIT TO CREATE NICKNAME

You might also like