#stringLoop.py #loop over indices of a string variable s = input("Input a string: ") print("Length of '" + s + "' is: " + str(len(s))) #print("Length of ",s," is:",len(s)) for i in range(len(s)): #indexes from 0 thru len(s)-1 print(i, ":", s[i], end=" ") if s[i].isalpha(): print("letter") elif s[i].isdigit(): print("digit") else: print() print("s[6]:",s[6]) #doesn't have to be in a loop print("Another way to access each character of a string.") for ch in s: print(ch) #search print("Search for a character in the string") c = input("Enter the character to search for: ") if c in s: print("found it with 'in'") else: print("didn't find it with 'in'") for i in range(len(s)): if c == s[i]: print("found it at index:", i) #finds all of them print("Find:", s.find(c)) #index where first found. returns -1 if not found if s.find(c) != -1: print("found it with find") else: print("didn't find it with find") #reverse the string for i in range(len(s)-1,-1,-1): print(s[i], end="") print() #slice substring start = eval(input("Slice start index: ")) stop = eval(input("Slice stop index: ")) print(s[start:stop]) #not an error if beyond end of string? #upper it print(s.upper()) print("s unchanged:",s) s_upped = s.upper() print(s_upped) s = s.upper() print("s is now uppered:",s) #strings are immutable, i.e. can't be modified. #s[2] = 'X' #will be error, so try it. print(min(s)) print(max(s)) #print(sum(s)) #can't 'sum' a string