Categories
Python Answers

How to convert list to string with Python?

Sometimes, we want to convert list to string with Python.

In this article, we’ll look at how to convert list to string with Python.

How to convert list to string with Python?

To convert list to string with Python, we can use the string join method.

For instance, we write

list1 = ['1', '2', '3']
str1 = ''.join(list1)

to call the empty string’s join method with list list1 to return a string that has all the strings in list1 combined into a string.

Conclusion

To convert list to string with Python, we can use the string join method.

Categories
Python Answers

How to use raw_input without pressing enter in Python?

Sometimes, we want to use raw_input without pressing enter in Python.

In this article, we’ll look at how to use raw_input without pressing enter in Python.

How to use raw_input without pressing enter in Python?

To use raw_input without pressing enter in Python, we can use the pynput library.

To install it, we run

pip install pynput

Then we use it by writing

from pynput import keyboard

print('Press s or n to continue:')

with keyboard.Events() as events:
    event = events.get(1e6)
    if event.key == keyboard.KeyCode.from_char('s'):
        print("hello")

We use keyboard.Events to create an event object.

Then we get the keyboard event with

event = events.get(1e6)

We then check if the s key pressed with

event.key == keyboard.KeyCode.from_char('s')

If it is, then we print 'hello'.

Otherwise, pynput will block the program until s is pressed.

Conclusion

To use raw_input without pressing enter in Python, we can use the pynput library.

Categories
Python Answers

How to access command line arguments with Python?

Sometimes, we want to access command line arguments with Python.

In this article, we’ll look at how to access command line arguments with Python.

How to access command line arguments with Python?

To access command line arguments with Python, we can use the sys.argv property.

For instance, we write

import sys

print(sys.argv)

to print out all the command line arguments in a list with print and sys.argv.

Conclusion

To access command line arguments with Python, we can use the sys.argv property.

Categories
Python Answers

How to obfuscate Python code?

Sometimes, we want to obfuscate Python code.

In this article, we’ll look at how to obfuscate Python code.

How to obfuscate Python code?

To obfuscate Python code, we can use the py_compile option with python.

To use it, we run

python -OO -m py_compile foo.py

to run python with py_compile to compile foo.py into a .pyo file.

A .pyo file is a Python script file that has the obfuscated version of the original code.

Some of the variables names can be recognized in the file and comments and docstrings are stripped out.

Conclusion

To obfuscate Python code, we can use the py_compile option with python.

Categories
Python Answers

How to get multiline input from user with Python?

Sometimes, we want to get multiline input from user with Python

In this article, we’ll look at how to get multiline input from user with Python.

How to get multiline input from user with Python?

To get multiline input from user with Python, we can call input in an infinite loop.

For instance, we write

contents = []
while True:
    try:
        line = input()
    except EOFError:
        break
    contents.append(line)

to call input in the infinite while loop to get its input value.

Then we catch the EOFError and stop the look with break if there’s an EOFError raised.

The error will be raised if Ctrl-D or Ctrl-Z is pressed.

Outside the try-except statement, we call contents.append to append the entered line into the contents list.

Conclusion

To get multiline input from user with Python, we can call input in an infinite loop.