Basic String Methods

Basic String Methods

sentence = 'something'

sentence.upper() # Upercases the sentance variable

print(sentence) # even though the method .upper ran the string within the sentence variable is still lowercase

# To get that data working you need to assign it to a variable:
up_sen = sentence.upper
# Or re-assign it to itself
sentence = sentence.upper()

Every time you see () we are working with a function or a method

Difference between function and Method:

  • if there is a . before the word then it is a method

    • example: sentence.upper()

    • methods also are associated with an object

  • if it is stand alone then it is a function

    • example: print()

    • functions are stand alone

Some Types of methods:

  • .upper - Uppercase letters

  • .lower - Lowercase letters

  • .capitalize - Capitalizez first letter and lowercase the rest

  • .isdigit - Checks if the string is a digit and returns a boolean value (True or False)

  • There are multiple methods starting with is - Used to check: is something like that?

  • There are multiple methods starting with startswith()

    • expects string

  • Similar to startswith there is also endswith()

    • expects string

Last updated