>>> st = "hello world" >>> ' '.join(format(ord(x), 'b') for x in st) '1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100' #using `bytearray` >>> ' '.join(format(x, 'b') for x in bytearray(st, 'utf-8')) '1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'
Here is what the above code is Doing:
1. `st` is a string.
2. `ord(x)` returns the unicode code point of the character `x`.
3. `format(ord(x), ‘b’)` returns the unicode code point of `x` in binary.
4. `’ ‘.join(format(ord(x), ‘b’) for x in st)` joins the binary representations of each character in `st` with a space.
The second example is similar, but uses `bytearray` instead of `ord`.
## How to convert a string to a list of characters
You can convert a string to a list of characters using the `list` function:
>>> list(“hello”)
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
You can also use a list comprehension:
>>> [x for x in “hello”]
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
## How to convert a list of characters to a string
You can convert a list of characters to a string using the `join` method of the string class:
>>> “”.join([“h”, “e”, “l”, “l”, “o”])
‘hello’
## How to convert a string to a list of words
You can convert a string to a list of words using the `split` method of the string class:
>>> “hello world”.split()
[‘hello’, ‘world’]
## How to convert a list of words to a string
You can convert a list of words to a string using the `join` method of the string class:
>>> ” “.join([“hello”, “world”])
‘hello world’
## How to convert a string to a list of lines
You can convert a string to a list of lines using the `splitlines` method of the string class:
>>> “hello\nworld”.splitlines()
[‘hello’, ‘world’]
## How to convert a list of lines to a string
You can convert a list of lines to a string using the `join` method of the string class:
>>> “\n”.join([“hello”, “world”])
‘hello\nworld’
## How to convert a string to uppercase
You can convert a string to uppercase using the `upper` method of the string class:
>>> “hello world”.upper()
‘HELLO WORLD’
## How to convert a string to lowercase
You can convert a string to lowercase using the `lower` method of the string class:
>>> “HELLO WORLD”.lower()
‘hello world’
## How to convert a string to titlecase
You can convert a string to titlecase using the `title` method of the string class:
>>> “hello world”.title()
‘Hello World’
## How to convert a string to camelcase
You can convert a string to camelcase using the `title` method of the string class, followed by the `replace` method:
>>> “hello world”.title().replace(” “, “”)
‘HelloWorld’
## How to convert a string to snakecase
You can convert a string to snakecase using the `lower` method of the string class, followed by the `replace` method:
>>> “Hello World”.lower().replace(” “, “_”)
‘hello_world’
## How to convert a string to kebabcase
You can convert a string to kebabcase using the `lower` method of the string class, followed by the `replace` method:
>>> “Hello World”.lower().replace(” “, “-“)
‘hello-world’
## How to convert a string to reverse case
You can convert a string to reverse case using the `swapcase` method of the string class:
>>> “Hello World”.swapcase()
‘hELLO wORLD’
## How to convert a string to alternating case
You can convert a string to alternating case using the `swapcase` method of the string class, followed by the `upper` method:
>>> “Hello World”.swapcase().upper()
‘HELLO wORLD’
## How to convert a string to inverse case
You can convert a string to inverse case using the `swapcase` method of the string class, followed by the `lower` method:
>>> “Hello World”.swapcase().lower()
‘hello world’
## How to