# Single Quote: Insert string value inside single inverted commas.
text = 'A strings can be single quoted'
# Double Quote: Can be useful for escaping single inverted comma(')
# rest there is no difference.
text = "This string's under double quotes"
# Triple Quote: Used for multi-line text, can be used with
# single/double inverted commas.
# You can use opposite inverted commas to escape them.
text = """This is a long text.
And want to use multiple lines."""
print("normal str,\t escaping characters") # normal str, escaping characters
print(r"\n raw string \t no escaping characters") # \n raw string \t no /
# escaping characters
## Unicode type, it represents english and non-english characters
# or other symbols
print(u"This is unicode, also can be in हिन्दी.") # This is unicode,
# also can be in हिन्दी. From python 3.0 and above, str object can contain Unicode
# characters, so u'' is optional.
# Note: Make sure you have selected utf-8 encoding in your code editor
print("\U0001F40D") # 🐍
# Also variable names support Unicode characters
संख्या = 34
print(संख्या) # 34
## Formatting type, used to pass python expression/variable inside a string
# below is different ways to create the same string output
n = 1
text = f"This is a String number {n}" # f-string to pass variable
text = f"This is a String number {20-19}" # or even to pass expression
text = "This is a String number %s" % n # or C like formatting
text = "This is a String number {0}".format(n) # or format method of string
print(text) # This is a String number 1
## Multiplying: Use the '*' operator on string to replicate to the count.
string1 = "this" * 5
print(string1) # thisthisthisthisthis
## Joining: Use the '+' operator to join two or more strings,
# they should be str. str is immutable, so a new string is
# created when joining.
string1 = "This is 1."
string2 = "This is 2."
new_string = string1 + string2
print(new_string) # This is 1.This is 2.
sample_str = "This contain some characters"
## Indexing: Accessing item/character from string using indexes.
# indexs begin with 0 (like everywhere!) to n-1 (n is length of str)
print(sample_str[0]) # T
print(sample_str[2]) # i
# Negative indexing: Access string characters from end of the string.
# It starts from 1 and not 0, so the last character is -1
print(sample_str[-1]) # s
print(sample_str[-5]) # c
## Iterating: Going over item by item from a string.
for x in sample_str:
print(x) # "This contain some characters"
## Slicing: For creating substrings, syntax is [start_index:end_index:step].
# 1. start_index: is starting index of substring, default is 0
# 2. end_index: is ending index, (end_index - 1) is considered,
# default is last index i.e length of string
# 3. step: is the gap between characters, default is 1
my_string = "This is some string."
print(my_string[5:7]) # is
# not providing end_index will use default i.e length - 1
# so below line is same as my_string[5:20]
print(my_string[5:]) # is some string.
# not providing start_index will use default i.e 0
# so below line is same as my_string[0:4]
print(my_string[:4]) # This
# using 2 step, missing one character after a character
print(my_string[::2]) # Ti ssm tig
# using negative indexing
# so below line is same as my_string[0:16]
print(my_string[:-4]) # This is some str
# reverse a string
print(my_string[::-1]) # .gnirts emos si sihT
my_string = "this IS it."
# Returns Lowercases all characters of given string
print(my_string.lower()) # this is it.
# Returns Uppercases all characters of given string
print(my_string.upper()) # THIS IS IT.
# Returns Capitalizing first character string
print(my_string.capitalize()) # This is it.
# Splits at a given string key(which is whitespace here) and
# returns list of strings
print(my_string.split(" ")) # ['this', 'IS', 'it.']
# Removes whitespace from beginning, also can strip given another key string
print(my_string.strip()) # this IS it.
# Searches given key/string(which is 'it' here) and
# returns starting index if found
print(my_string.index("it")) # 8
# Searches given key string(which is 'it' here), replaces with
# second key(which is 'not it' here) string and then returns the final string
print(my_string.replace("it","not it")) # this IS not it.
# joins a list of strings to a single string using a given string
# key(which is '.' here)
print(".".join(['hey','is','this','it?'])) # hey.is.this.it?
my_string = "this IS it."
# Returns the length of a string
print(len(my_string)) # 11
# Returns the string representation of any object,
# for str object returns a string with no escaping characters
print(repr("This \t should have escaped.")) # 'This should have escaped.'
# Returns a Unicode of a character
print(ord("c")) # 99
# Returns converted the Unicode to a character
print(chr(ord("c"))) # c
my_string = "bar"
my_string1 = "20"
# str to int
print(int(my_string)) # ValueError
print(int(my_string1)) # 20
# str to float
print(float(my_string)) # ValueError
print(float(my_string1)) # 20.0
# str to bytes
print(bytes(my_string, encoding='utf-8')) # b'bar'