In Python strings can be created with single or double quotes or triple single quotes (for multiline string):
scripts/strings.pygreeting = "hello"
greeting2 = 'hi there'
greeting3 = '''hello there!
how are you doing'''
print(greeting)
print(greeting2)
print(greeting3)
# getting type
print(type(greeting))
Outputhello hi there hello there! how are you doing <class 'str'>
Formatting strings
In Python we can format strings by using format() function
definition of format()
def format(self, *args, **kwargs):
Example:
scripts/strings-format-example.pygreeting = "Hi {name}, greeting from {lang}!"
formatted = greeting.format(name="Joe", lang="Python")
print(formatted)
OutputHi Joe, greeting from Python!
F-strings
F-string does the same thing what str.format() does. F-strings are string literals that are prefixed by the letter 'f' or 'F'. This syntax can be desirable if string literals need to support interpolation:
scripts/strings-f-string-example.pyname = "Joe"
lang = "Python"
greeting = F"Hi {name}, greeting from {lang}!"
print(greeting)
OutputHi Joe, greeting from Python!
Example ProjectDependencies and Technologies Used: |