30 seconds of python

String

Click on a snippet's name to view its code.

byte_size

Returns the length of a string in bytes.

Use s.encode('utf-8') to encode the given string and return its length.

camel

Converts a string to camelcase.

Use re.sub() to replace any - or _ with a space, using the regexp r"(_|-)+". Use title() to capitalize the first letter of each word convert the rest to lowercase. Finally, use replace() to remove spaces between words.

capitalize

Capitalizes the first letter of a string.

Capitalize the first letter of the string and then add it with rest of the string. Omit the lower_rest parameter to keep the rest of the string intact, or set it to True to convert to lowercase.

capitalize_every_word

Capitalizes the first letter of every word in a string.

Use s.title() to capitalize first letter of every word in the string.

decapitalize

Decapitalizes the first letter of a string.

Decapitalize the first letter of the string and then add it with rest of the string. Omit the upper_rest parameter to keep the rest of the string intact, or set it to True to convert to uppercase.

is_anagram

Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).

Use s.replace() to remove spaces from both strings. Compare the lengths of the two strings, return False if they are not equal. Use sorted() on both strings and compare the results.

kebab

Converts a string to kebab case.

Break the string into words and combine them adding - as a separator, using a regexp.

n_times_string

Prints out the same string a defined number of times.

Repeat the string n times, using the * operator.

palindrome

Returns True if the given string is a palindrome, False otherwise.

Use s.lower() and re.sub() to convert to lowercase and remove non-alphanumeric characters from the given string. Then, compare the new string with its reverse.

reverse_string

Returns the reverse of a string.

Use string slicing to reverse the string.

snake

Converts a string to snake case.

Break the string into words and combine them adding _ as a separator, using a regexp.

split_lines

Splits a multiline string into a list of lines.

Use s.split() and '\n' to match line breaks and create a list.