Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
format() is way better than % in many different ways. One of those is:
>>> names = ['ali', 'reza', 'alireza', 'mohsen']
>>> print map('Hello Mr. {}'.format, names)
['Hello Mr. ali', 'Hello Mr. reza', 'Hello Mr. alireza', 'Hello Mr. mohsen']

#python #format #string_formatter #map
Some useful string methods, variables:

string.ascii_letters: in case you want to use alpha characters a-zA-Z. The output is:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

string.ascii_lowercase:
abcdefghijklmnopqrstuvwxyz

string.ascii_uppercase:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

string.digits output is numbers:
0123456789

string.punctuation output is special charaters:
!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~

string.whitespace includes all whitespace charaters:
\t\n\x0b\x0c\r

NOTE: space is at the end of the above output, which is not visible.

There are many usecases you can do with this list like generating password, invitation code and etc from a psecific list.

#python #string #ascii_lowercase #ascii_uppercase #digits #punctuation #whitespace
Tech C**P
Some useful string methods, variables: string.ascii_letters: in case you want to use alpha characters a-zA-Z. The output is: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ string.ascii_lowercase: abcdefghijklmnopqrstuvwxyz string.ascii_uppercase:…
Generate a sample string (bonus coupon) from string functions and removing misguiding characters from the final list:
def generate_coupon(coupon_code_length=10):
alphanum = list(string.ascii_uppercase + string.digits)
alphanum.remove('L')
alphanum.remove('I')
alphanum.remove('1')
alphanum.remove('0')
alphanum.remove('O')

return ''.join(random.choice(alphanum) for i in range(coupon_code_length))

This function returns a random string from ascii_uppercase and string.digits. This is one usages out of very many usages of string module public variables. :)

GET THE IDEA!

#python #string #ascii_uppercase #digits #random #random_choice #range
How to replace multiple characters in a string using python?

As you may already know there is a method for string called replace that replaces 1 character with a new given character:
'+98 21 445 54 12'.replace('+', '')
# output is 98 21 445 54 12

But what if you want to replace both + and [SPACE] inside of the string? The answer is simple just chain the methods:
'+98 21 445 54 12'.replace('+', '').replace(' ', '')

Here output would be 98214455412.

#python #string #replace #multiple_replacement
How to reverse a string in python?

By slicing it is easy as pie! The general format of string slicing is like below:

'YOUR_STRING'[begin : end : step]

We do a little bit of a magic here and make step -1. -1 will read data from the end of the string to the first character and leave begin and end intact:

'Hello'[::-1]

The output would be like below:

>>> 'hello'[::-1]
'olleh'

#python #string #slicing #step #reverse
How to turn a dictionary into a string?

extra_data = {'iso': 'IR', 'address': 'Iran - Tehran - Azadi'}
' - '.join('{}:{}'.format(key, val) for key, val in extra_data.items())

The output would be:

iso:IR - address:Iran - Tehran - Azadi

NOTE: it could come in handy in case you want to store a variable dictionary structure into NO-SQL database.

#python #dictionary #string