name = "Thomas Jefferson" print("first letter is ", name[0]) print("last letter is ", name[len(name)-1]) print() # print an empty line to separate sets of examples # slicing with [start:stop ] and return a part of the string # from start index, upto (but not include) stop index print("name[ :6] = ", name[ :6]) print("name[7: ] = ", name[7: ]) print("name[ :-3] = ", name[ :-3]) # throw away the last 3 characters print("name[-3: ] = ", name[-3:]) # start from the last 3 characters to the end print("name[-9:-3] = ", name[-9:-3]) # throw away the last 3 characters # start from the last 9 characters to the end print() # print an empty line to separate sets of examples # more slicing example string = 'CS1111 Intro to programming' print("original string is", string) print("slice from index 7 upto but not include index 15 '" + string[7:15] + "'") print() print("Jeff" in name) print("upsorn" not in name) print() # print an empty line to separate sets of examples # string is immutable # once assign a string, we cannot mutate part of the string # name[3] = "M" # error # print(name) print(name.startswith("Th")) print(name.endswith("son")) print(name.isdigit()) print(name.isspace()) print(name.isalnum()) print(name.isalpha()) print(name.find("f")) print(name.find("cs1111")) print(name.rfind("f")) print(name.rfind("cs1111")) print(name.count("f")) print(name.count("ff")) print(name.islower()) print(name.isupper()) print(name.replace("Thomas", "George")) print() # print an empty line to separate sets of examples print("The name is \"" + name + "\"") # escape character print("Thomas\nJefferson") # new line character print() # print an empty line to separate sets of examples # string equality: # same length, same sequence of characters # case sensitive name1 = "Thomas Jefferson" name2 = "Thomas Jefferson" name3 = "Thomas jefferson" name4 = "Thomas Jenkinson" print(name1 == name2) print(name1 == name3) print(name1 == name4) print() # print an empty line to separate sets of examples seq_list = ["$", "--", "#"] print("join list with string :", name.join(seq_list)) print("join list with empty string :", "".join(seq_list)) print() # print an empty line to separate sets of examples print("name = ", name) print("split name = ", name.split()) print() # print an empty line to separate sets of examples str1 = "Thomas Jefferson" str2 = "e" print("index of \"e\" from a given string:", str1.index(str2)) print("index of \"e\" start from index 9 to the end :", str1.index(str2, 9)) print("index of \"e\" start from index 9 upto index 12 :", str1.index(str2, 9, 12)) # print(str1.index(str2, 9, 11)) # from index 9, upto index 11 (not include index 11) --> error # more index() examples string = 'CS1111 Intro to programming' print("index of 'n' in string :", string.index('n')) print("index of '1111' in string :", string.index('1111')) print("index of '1' in string :", string.index('1')) # the first index found print() # print an empty line to separate sets of examples print("replace() returns as copy of string :", name.replace("Thomas", "George")) # return a copy of string