Skip to content

Tag: string

Python split string in moving window

I have a string with digits like so – digit = “7316717” Now I want to split the string in such a way that the output is a moving window of 3 digits at a time. So I get – [“731”, “316”, “167”, “671”, “717”] How would the approa…

Joining pairs of elements of a list [duplicate]

This question already has answers here: How to iterate over a list in chunks (39 answers) Closed 8 months ago. I know that a list can be joined to make one long string as in: Obviously this would output: However, what I am trying to do is simply join the first and second strings in the list, then join the

Can you have variables within triple quotes? If so, how?

This is probably a very simple question for some, but it has me stumped. Can you use variables within python’s triple-quotes? In the following example, how do use variables in the text: I would like it to result in: If not what is the best way to handle large chunks of text where you need a couple varia…

Titlecasing a string with exceptions

Is there a standard way in Python to titlecase a string (i.e. words start with uppercase characters, all remaining cased characters have lowercase) but leaving articles like and, in, and of lowercased? Answer There are a few problems with this. If you use split and join, some white space characters will be ig…

How to insert a character after every 2 characters in a string

Is there a pythonic way to insert an element into every 2nd element in a string? I have a string: ‘aabbccdd’ and I want the end result to be ‘aa-bb-cc-dd’. I am not sure how I would go about doing that. Answer Assume the string’s length is always an even number, The t can also be…