Strings

String Method Description Example
+ Concatenates "Hello" + "World" = "Hello World"
str() Converts an number to a string str(5) = "5"
len() Returns the length of a string len("Hello World")= 11
capitalize() Makes the first character upper case txt="hello world"
txt.capitalize()="Hello world"
center(length, character) Centers the string in the amount of space specified by the length parameter. Optional Character specifies character to use. White space is the default. txt="hello world"
txt.center(20,"!")= "!!!!hello world!!!!!"
count() Returns the number of times a string occurs in another string txt="Cats are smarter then dogs. Cats rule."
txt.count("cats")= 2
find(str_to_find, startpoint, endpoint) Finds a string in a string. Begins at starpoint which defaults to 0 and searches to endpoint which defaults to length of the string. txt="Cats are smarter then dogs. Cats rule."
txt.find("Cat")= 0
txt.find("so")= 12
txt.find("Cat", 10)= 36
txt.find("fish")= -1
format(parameter1, parameter2, ...) Allows you to put variables into your string. txt="Cats are {} then dogs. Cats {}."
txt.format("smarter","rule")= Cats are smarter then dogs. Cats rule
txt="Cats are {1} then dogs. Cats {0}."
txt.format("rule","smarter")= Cats are smarter then dogs. Cats rule
lower() Makes every character lower case txt="HELLO World"
txt.capitalize()"hello world"
replace(oldvalue, newvalue) replaces any occurrences of the old value with the new value in the string txt="I think dogs are great. I love dogs."

txt.replace("dogs","cats") = "I think cats are great. I love cats ."

strip() Removes any white space from the beginning or end of the string txt=" Hello world "
txt.strip()="Hello world"
title() Makes the first character of every word upper case txt="hello world"
txt.title()="Hello World"
upper() Makes everything upper case txt="hello world"
txt.upper()="HELLO WORLD"

Comments

  • Single line comments use the #
  • Multi line comments use ''' before and after
  • Comment can be in a line of code.
'''I am a multiple line comment. 
See how I use three single quotes
 before and after?'''
print("hi")
#I am a single line of comment
counter = 1  #Comment on a line

Lists

Create lists using square brackets:

num_list = [1, 5, 11, 17]
string_list = ['hi', 'hey', 'hello']

Print the list:

print(num_list)
The output will be: [1, 5, 11, 17]

Access items - first item is in the 0 position!

print(string_list[0])
The output will be: hi

Change items

string_list[0] = "yo"

Add item to the end of the list

string_list.append("What's Up?")

Add item to a specified spot in the list

string_list.append(2,"What's Up?")

Remove item

del string_list[3]

Lists - Part 2

Checking to see if Item is in list
Using the in reserved word - returns True if the item is in the list and False if not.

if "hey" in string_list:
   print("Yep, hey is in there")

Using the index method to see where the item is

num_list = [1, 5, 11, 17]
print(num_list.index("5"))
The output will be:1

Length of List use len()

list_len=len(winter_list)
print("This list has ",list_len,"items in it)

Sort list use .sort()

print("string_list.sort()

Reverse Sort list use .sort()

print("string_list.sort(reverse=True)