oneliner:
>>> s="badcfe"
>>> ''.join([ s[x:x+2][::-1] for x in range(0, len(s), 2) ])
'abcdef'
- s[x:x+2] returns string slice from x to x+2; it is safe for odd len(s).
- [::-1] reverses the string in Python
- range(0, len(s), 2) returns 0, 2, 4, 6 … while x < len(s)
Related Contents:
- Does Python have a string ‘contains’ substring method?
- Check if multiple strings exist in another string
- Remove specific characters from a string in Python
- How are strings compared?
- How to check if a string is a substring of items in a list of strings?
- How to split a string into a list of characters?
- How to format a floating number to fixed width in Python
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python 3
- Simpler way to create dictionary of separate variables?
- TypeError: Can’t convert ‘int’ object to str implicitly
- How slow is Python’s string concatenation vs. str.join?
- Aren’t Python strings immutable? Then why does a + ” ” + b work?
- Checking whether a string starts with XXXX
- What does preceding a string literal with “r” mean? [duplicate]
- Python regex – r prefix
- Case insensitive ‘in’
- Split a string at uppercase letters
- How to check if type of a variable is string?
- How to add a string in a certain position?
- Remove characters except digits from string using Python?
- How to split strings into text and number?
- How to get integer values from a string in Python?
- How to remove the left part of a string?
- inheritance from str or int
- Alphabet range in Python
- How to insert a character after every 2 characters in a string
- Splitting a semicolon-separated string to a dictionary, in Python
- Convert Variable Name to String?
- How can I escape latex code received through user input?
- How do I write data into CSV format as string (not file)?
- Most pythonic way to interleave two strings
- Python Replace \\ with \
- How do I format a string using a dictionary in python-3.x?
- I have a string whose content is a function name, how to refer to the corresponding function in Python?
- Using “and” and “or” operator with Python strings [duplicate]
- Why doesn’t calling a string method modify (mutate) the string? Why doesn’t it change unless I assign the result?
- Why does Python not perform type conversion when concatenating strings?
- How do I change the string representation of a Python class? [duplicate]
- Format string unused named arguments [duplicate]
- How to concatenate (join) items in a list to a single string
- Mass string replace in python?
- ValueError: invalid literal for int() with base 10: ‘stop’
- Finding subsequence (nonconsecutive)
- Determine the common prefix of multiple strings
- What is a “bytestring” (the `bytes` data type) in Python?
- Splitting List That Contains Strings and Integers
- Length of longest word in a list
- Split string at nth occurrence of a given character
- Reverse a string without using reversed() or [::-1]?
- Why are str.count(”) and len(str) giving different outputs when used on an empty string?