You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
strings="This is Python"char="C"multiline_str="""This is a multiline string with more thanone line code."""unicode=u"\u00dcnic\u00f6de"raw_str=r"raw \n string"print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
ThisisPythonCThisisamultilinestringwithmorethanonelinecode.
Ünicöderaw \nstring
Quoting and manipulation examples
print("Strings separated by white space "'are concatenated.'''' Including triple quoted, which may have embedded line returns and whitespace.''')
# Strings separated by white space are concatenated. Including triple quoted,# which may have embedded line returns and whitespace.print("Concatenation usefully splits ""long strings onto multiple lines.")
# Concatenation usefully splits long string literals onto multiple lines.print("""\The backslash above usefully makes the content all on the same line""")
# The backslash above usefully makes the content all on the same lineprint(3*"I'm tripled "+", and I'm concatenated.")
# I'm tripled I'm tripled I'm tripled , and I'm concatenated.this_string_variable='This string variable'print( this_string_variable+" (and some Built-in functions) be concatented with this string literal.")
# This string variable (and some Built-in functions) be concatented with this string literal.word='Py'word+='thon'# 'Python' Concatenation, then re-assignment, NOT string mutationprint(word[0]) # 'P' 1st characterprint(word[2]) # '3' 6th characterprint(word[-1]) # 'n' last characterprint(word[-2]) # 'o' 2nd-last characterprint(word[-6]) # 'P' 1st characterprint(word[0:2]) # 'Py'characters from position 0 (included) to 2 (excluded)print(word[::2]) # 'Pto' every second characterprint(word[2:99]) # 'thon' out of range slice indexes are handled gracefullyprint(len(word)) # '6' String length# These all invalid;# word[99] - IndexError: string index out of range# word[0] = 'J' - Strings immutability -># TypeError: 'str' object does not support item assignmentprint( f"{word}'s f-string") # 'Python's f-string'
Formatting
'hi {} from {}'.format(x,y)
'hi {b} from {a}'.format(a=" ",b=" ")
sprintf style - 'The value of x is %3.2f' %12.3456789
Handy-dandy _to_low_camel_case function
This is a useful function I've needed a few times.
Automatically converts strings if the parts are broken up by any non-alphanumeric characters like "_", " ", "-" . Eg. FLOOD_LIGHT_CONFIG would become floodLightConfig.
By default only converts if some non-alphanumeric delimiters are present to prevent something like floodLightConfig becoming floodlightconfig
def_to_lower_camel_case(string: str, delimiters_required=True) ->str:
fromreimportsplitifdelimiters_requiredandstring.isalnum():
# String doesn't contain any delimiters to identify words, so skip conversionreturnstringreturn"".join(
part.lower() ifi==0elsepart.title()
fori, partinenumerate(split("([^a-zA-Z0-9])", string))
ifpart.isalnum()
)
``