Strings like "abc" are not officially of type list, but... a string is, in essence, a list of characters. You can loop through a string like you do a list. Take for instance this code to count how many 'a' characters are in a string: i=0 i=i+1count=0 while i<len(astring): # do something with each character if astring[i]=='a': count=count+1 You can not modify a string by using an index: in this sense, strings are immutable. You can get a slice of a string. For instance, the following code gets the first two characters of a string: astring=astring[0:2] To write a function to, say, replace the ith character of a string, you'd have to use string slices: def replace(astring,i,replacement): return astring[0:i]+replacement+astring[i+1:len(astring)] You can build a string similar to how you build a list:
asterisk = '*' * count gives a string of asterisks count long. |