Categories
Python Answers

How to trim whitespace with Python?

Sometimes, we want to trim whitespace with Python.

In this article, we’ll look at how to trim whitespace with Python.

How to trim whitespace with Python?

To trim whitespace with Python, we can use the strip, rstrip or lstrip methods.

For instance, we write

s = "  \t a string example\t  "
s = s.strip()

to return a string with the whitespaces at the start and the end of string s stripped out.

If we just want to trim the whitespaces at the end of a string, we write

s = s.rstrip()

And if we just want to trim the whitespaces at the start of a string, we write

s = s.lstrip()

Conclusion

To trim whitespace with Python, we can use the strip, rstrip or lstrip methods.

Categories
Python Answers

How to modify a Python dict while iterating over it?

Sometimes, we want to modify a Python dict while iterating over it.

In this article, we’ll look at how to modify a Python dict while iterating over it.

How to modify a Python dict while iterating over it?

To modify a Python dict while iterating over it, we can use the items method to get the key and value.

For instance, we write

prefix = 'item_'
t = {'f1': 'ffw', 'f2': 'fca'}
t2 = dict() 
for k,v in t.items():
    t2[k] = prefix + v

to loop through the key value pairs in t with t.items() and the for loop.

In it, we set t2[k] to the prefix + v where v is the value in the t dict.

t2 is a new dict so we can modify it in the for loop.

Conclusion

To modify a Python dict while iterating over it, we can use the items method to get the key and value.

Categories
Python Answers

How to fix function not changing global variable with Python?

Sometimes, we want to fix function not changing global variable with Python.

In this article, we’ll look at how to fix function not changing global variable with Python.

How to fix function not changing global variable with Python?

To fix function not changing global variable with Python, we add the global keyword in front of the letter we want to change.

For instance, we write

def function():
    global done
    for loop:
        code
        if not comply:
            done = True

to define the done variable with global.

Then in the if block, we set the outside done variable to True since we have global in front of done.

Conclusion

To fix function not changing global variable with Python, we add the global keyword in front of the letter we want to change.

Categories
Python Answers

How to create a Caesar Cipher function in Python?

Sometimes, we want to create a Caesar Cipher function in Python.

In this article, we’ll look at how to create a Caesar Cipher function in Python.

How to create a Caesar Cipher function in Python?

To create a Caesar Cipher function in Python, we can use the string slicing feature.

For instance, we write

def caesar(plaintext, shift):
    alphabet = string.ascii_lowercase
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    table = string.maketrans(alphabet, shifted_alphabet)
    return plaintext.translate(table)

to define the caesar function that takes the plaintext string and the number of positions to shift the characters in plaintext.

In it, we get the lowercase alpgabets in a string with

alphabet = string.ascii_lowercase

Then we shift the alphabet with

shifted_alphabet = alphabet[shift:] + alphabet[:shift]

Then we call maketrans to create a dict that maps the letters in alphabet to the letters in shifted_alphabet.

And then we call plaintext.translate with table to return a new string that maps the letters in the keys of table to the corresponding letter value.

Conclusion

To create a Caesar Cipher function in Python, we can use the string slicing feature.

Categories
Python Answers

How to write a connection string when password contains special characters with Python SQLalchemy?

Sometimes, we want to write a connection string when password contains special characters with Python SQLalchemy.

In this article, we’ll look at how to write a connection string when password contains special characters with Python SQLalchemy.

How to write a connection string when password contains special characters with Python SQLalchemy?

To write a connection string when password contains special characters with Python SQLalchemy, we should URL encode our string.

For instance, we write

from urllib import quote_plus as urlquote
from sqlalchemy.engine import create_engine

engine = create_engine('postgres://user:%s@host/database' % urlquote('badpass'))

to call create_engine with a connection string that we create by call urlquote on the password string to URL encode the password.

And then we interpolate that into the connection string.

Conclusion

To write a connection string when password contains special characters with Python SQLalchemy, we should URL encode our string.