Python Iterate Over String



I want to be able to loop over 16 characters starting and print it. Then move up 1 letter, loop over 16 characters and print that. Until there isn't 16 characters left.

Any help on how i'd do this?


Something like this?

string = "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN" for n in range(len(string)-15): print(string[n:n+16])

You have to iterate over every character up to the last character that has 16 characters after it (so the length of the string, minus 15 (because indexing starts at 0) : len(string)-15), and then print the string sliced at that starting index up to the index + 16 (string[n:n+16]).

Slicing is an important and IMO powerful aspect of Python programming, it's a great read if you're new to the language (or programming in general) and you should definitely practice it. <a href="https://docs.python.org/2/tutorial/introduction.html#strings" rel="nofollow">The official docs</a> have some good information on the topic.


If I understood well, you might want to do something like:

a = "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN" for i in range(len(a) - 15): print(a[i:i+16])


I would do:

str = "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN" for i in range(len(str)): if len(str[i:i+16]) == 16: print(str[i:i+16]) else: break


How about a list comprehension?



  • Does compilng java code to exe (e.g. using Launch4Java) ensure code cannot be reversed engineered?
  • What should I use to create a Windows GUI?
  • How to build a AST for a proprietary language?
  • how to deepcopy a queue in python
  • Formula to remove entire words that start with certain characters
  • How can I create a list of elements with the same xpath using selenium with python?
  • KineticJS : scaling issue while using custom clipFunc on 2x pixel aspect display
  • How do I create closures for model getter-setter in angular?
  • Eclipse GUI building [duplicate]
  • Shortest route between multiple points in mapkit for iphone app
  • pandas mix position and label indexing without chaining
  • Alternate (Free) to AOP PostSharp for method tracing (and exception)
  • Displaying and sizing a grayscale from a QImage in Qt
  • Create ranking for vector of double
  • Rails Route parameters in AngularJS
  • PXAction seemingly does nothing
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • SQL - Select lowest values with group by and order by?
  • Cypher - matching two different possible paths and return both
  • can variables be set randomly when declaring them again?
  • C# - Most efficient way to iterate through multiple arrays/list
  • import scipy.sparse failed
  • DIV instruction jumping to random location?
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Diff between two dataframes in pandas
  • Moving Android View and preventing onDraw to be called over and over again
  • xtable package: Skipping some rows in the output
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Incrementing object id automatically JS constructor (static method and variable)
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Web-crawler for facebook in python
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • SQL merge duplicate rows and join values that are different
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file