name = 'Bruce Wayne'
Pseudo coding, writing comments, is done by writing after the # character. In python, a variable can be set to a particular datatype through a process called casting.
x = str('1') # prints '1' x = int(1) # prints 1
Type method is a python function that returns the datatype of a variable
print(type(name)) # prints <class 'str'>
Python's string datatype can be formed with single or double quotes. For a string that will run more than one line, they can be formed with three single or double quotes.
achilles = '''Let no man forget how menacing we are, we are lions! Do you know whats waiting beyond that beach? Immortality! Take it! It's yours!'''
If looking for a word inside of a string, the keyword in can be used.
print('lions' in achilles) # prints true
Can also use an if conditional to check for a word
sparta = 'SPARTANS! what is your profession?' if 'profession' in sparta: print('HA-OOH! HA-OOH! HA-OOH!') # prints HA-OOH! HA-OOH! HA-OOH!
Or can check if a word isn’t in a string with the not keyword
if 'cook' not in sparta : print('false') # prints false
To turn the characters in a string from lower case to upper case, theres an upper method.
upperCaseBatman = name.upper() print(upperCaseBatman) # prints BATMAN print(upperCaseBatman.lower()) # prints batman
Adding a string and a variable can be done with the addition operator.
def vengeance(): print('I am ' + name) vengeance() # prints I am Batman
Python has many methods for strings. These are just a few. The syntax is pretty easy to catch on. And I must admit, it does make python very easy to read. Also makes it look really clean.
Top comments (0)