A bit on what strings are and their METHODS (which make life easier) !! π₯π₯π₯
NOTE:
Almost all of the content in this article is taken from the official Python Docs, and divided into digestible chunks so that people like me can understand it (little by little).
This article is pretty straight forward. I tried to make it as crisp as possible.
It's more code and less talk cause:
Let's get started! ππ₯
What are Strings?
In Short:
Strings are a sequence of characters, used for storing Textual Data.
Examples β¨:
'Hello, World!' # single quotes
"This is an example(!) of a String" # double quotes
"Single 'quote' embedded in double quotes and vice versa is allowed"
'''Three single quotes and double are allowed! You can also embed 'single quotes' and "double quotes" just the way I did in this example.'''
Also,
They are Immutable i.e. They can't be modified, just like JavaScript π₯.
Some Very Common and Useful String (str) Methods with examples:
β capitalize, center (with docs)
β count, encode (with docs)
β endswith, expandtabs (with docs)
β find, format (with docs)
β index, isalnum, isalpha, isdigit, islower, isupper, isprintable, isspace, istitle (with (simplified) docs)
β join, ljust, lower, upper, rjust, lstrip, rstrip, strip, partition, removeprefix and removesuffix (Python 3.9), replace, split, startswith, swapcase, title, upper
# Common String (str) Methods in Python 3.9
# Note: Output is without quotes in prefix and suffix
s = "hello, world!"
# str.join(iterable):
# Return a string which is the concatenation of the strings in iterable.
print(" ".join(["hello", "world", "!"])) # 'hello world !'
# str.ljust(width[, fillchar]): (Remember "center" method?) Return the string left justified in a string of length width.
# Padding is done using the specified fillchar (default is an ASCII space).
print(s.ljust(20, "-")) # 'hello, world!-------'
# str.rjust(width[, fillchar]): same as ljust except it returns right justified string
print(s.rjust(20, "-")) # '-------hello, world!'
print(s.upper()) # HELLO, WORLD! (Returns All Characters Upper-cased)
print(s.lower()) # hello, world! (Returns All Characters Lower-cased)
print(s.title()) # Hello, World! (Returns Titledcase String)
print(s.lstrip()) # hello, world! (Returns Copy of String with Leading Characters (default=whitespaces) removed)
print(s.rstrip('!')) # hello, world (Returns Copy of String with trailing Characters removed)
print(' hello, world! '.strip())
# hello, world (Returns Copy of String with trailing and leading Characters removed) (default=whitespaces)
print(s.partition(',')) # ('hello', ',', ' world!')
# Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator,
# the separator itself, and the part after the separator.
# If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
print(s.split('l')) # ['he', '', 'o, wor', 'd!'] # Return a list of the words in the string,
# using sep as the delimiter string.
print(s.replace('l', 'L')) # 'heLLo, worLd!'
# str.replace(old, new[, count]): Return a copy of the string with all occurrences of substring old replaced by new.
# If the optional argument count is given, only the first count occurrences are replaced.
# ===============
# Python 3.9 β
# ===============
print(s.removeprefix("hello, ")) # "world!"
# If the string starts with the prefix string, return string[len(prefix):].
# Otherwise, return a copy of the original string:
print(s.removesuffix(", world!")) # "hello"
# If the string ends with the suffix string and that suffix is not empty,
# return string[:-len(suffix)]. Otherwise, return a copy of the original string
A little bit More β¨:
f-strings
f-strings in Python π:
βformatted string literals,β f-strings are string literals that have an f at the beginning and curly braces containing expressions that will be replaced with their values. [Source: realpython]
P.s.
Learn f-strings and write cleaner and more elegant code.17:03 PM - 12 Aug 2020
That's it! ππ₯
To Dig More into strings and stuff, click here (where most of the content of this article is taken from).
Thanks for reading. ππ»
Feel free to leave a comment if you want to add up something or suggest what I should share next :)
Top comments (4)
How did you get your code to look like that?
he's using carbon.now.sh/
I used Carbon.now.sh
Ok, thanks.