Let's say I have a list:
list = ["word", "word2", "word3"]
and I want to change this list to:
list = ["word:", "word2:", "word3:"]
is there a quick way to do this?
Use the strncat() function to append the character ch at the end of str. strncat() is a predefined function used for string handling. string.
To insert a character after every N characters, call the replace() method on the string, passing it the following regular expression - str. replace(/. {2}/g, '$&c') . The replace method will replace every 2 characters with the characters plus the provided replacement.
Method #2: Using append() This particular function can be used to perform the operation of appending a string element to the end of a list without changing the state of the string to a list of characters.
List comprehensions to the rescue!
list = [item + ':' for item in list]
In a list of
['word1', 'word2', 'word3']
This will result in
['word1:', 'word2:', 'word3:']
You can read more about them here.
https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With