How to generate a random alpha-numeric string

Algorithm To generate a random string, concatenate characters drawn randomly from the set of acceptable symbols until the string reaches the desired length. Implementation Here’s some fairly simple and very flexible code for generating random identifiers. Read the information that follows for important application notes. public class RandomString { /** * Generate a random string. … Read more

How do I pad a string with zeroes?

To pad strings: >>> n = ‘4’ >>> print(n.zfill(3)) 004 To pad numbers: >>> n = 4 >>> print(f'{n:03}’) # Preferred method, python >= 3.6 004 >>> print(‘%03d’ % n) 004 >>> print(format(n, ’03’)) # python >= 2.6 004 >>> print(‘{0:03d}’.format(n)) # python >= 2.6 + python 3 004 >>> print(‘{foo:03d}’.format(foo=n)) # python >= 2.6 … Read more

Easiest way to convert int to string in C++

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string. #include <string> std::string s = std::to_string(42); is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword: auto s = std::to_string(42); … Read more

Why is it string.join(list) instead of list.join(string)?

It’s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the “joiner” must be strings. For example: ‘_’.join([‘welcome’, ‘to’, ‘stack’, ‘overflow’]) ‘_’.join((‘welcome’, ‘to’, ‘stack’, ‘overflow’)) ‘welcome_to_stack_overflow’ Using something other than strings will raise the following error: TypeError: sequence item 0: expected str instance, int found

How to read a file line-by-line into a list?

This code will read the entire file into memory and remove all whitespace characters (newlines and spaces) from the end of each line: with open(filename) as file: lines = file.readlines() lines = [line.rstrip() for line in lines] If you’re working with a large file, then you should instead read and process it line-by-line: with open(filename) … Read more

How do I print curly-brace characters in a string while using .format?

You need to double the {{ and }}: >>> x = ” {{ Hello }} {0} ” >>> print(x.format(42)) ‘ { Hello } 42 ‘ Here’s the relevant part of the Python documentation for format string syntax: Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is … Read more

When should I use double or single quotes in JavaScript?

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string. Using the other type of quote as a literal: alert(‘Say “Hello”‘); alert(“Say ‘Hello'”); This can get complicated: alert(“It’s \”game\” time.”); alert(‘It\’s “game” time.’); Another option, new … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)