Using triple quotes and the line continuation character ( \ ) Defining a string on multiple lines.
a = ”’this\
is\
my\
first\
string”’

Using triple quotes and the line continuation character ( \ ) Defining a string on multiple lines.
a = ”’this
is
my
first
string”’

Strings – indexing
a.index("i")

Strings – character count
a.count("i")

Strings – finding a character
a.find("elu")

Strings – converting the case
a.lower() #lowercase a.upper() #uppercase

Strings – checking whether the string starts with a character
a.startswith("b")

Strings – checking whether the string ends with a character
a.endswith("h")

Strings – removing a character from the beginning and the end of a string
a = ” nithin eluvathingal ”
a.strip() #remove whitespaces

b = “$$$bingo$$$”
b.strip("$") #remove a certain character

Strings – removing all occurences of a character from a string
a.replace(" ", "") #replace each space character with the absence of any character

Strings – splitting a string by specifying a delimiter; the result is a list
a = "Nithin,Eluvathingal" #the delimiter is a comma a.split(",")

Strings – inserting a character in between every two characters of the string / joining the characters by using a delimiter
"_".join(a)

#Additional methods (source: https://www.tutorialspoint.com/python3/python_strings.htm)
Capitalizes first letter of string.
a. capitalize()

Removes all leading whitespace in string.
a.lstrip()

Removes all trailing whitespace of string.
a.rstrip()

Inverts case for all letters in string.
a.swapcase()

Returns “titlecased” version of string, that is, all words begin with uppercase and the rest are lowercase.
a.title()

Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.
a.isalnum()

Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.
a.isalpha()

Returns true if string contains only digits and false otherwise.
a.isdigit()

Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise.
a.islower()

Returns true if a unicode string contains only numeric characters and false otherwise.
a.isnumeric()
Returns true if string contains only whitespace characters and false otherwise.
a.isspace()

Returns true if string is properly “titlecased” and false otherwise.
a.istitle()

Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise.
a.isupper()

Strings – concatenating two or more strings
a = "nithin" b = "elu" a + b

Strings – repetition / multiplying a string
a = "elu" a * 3

Strings – checking if a character is or is not part of a string
a = "elu" "o" in a "b" not in a

Strings – formatting
Sr.No. | Format Symbol & Conversion |
---|---|
1 | %ccharacter |
2 | %sstring conversion via str() prior to formatting |
3 | %isigned decimal integer |
4 | %dsigned decimal integer |
5 | %uunsigned decimal integer |
6 | %ooctal integer |
7 | %xhexadecimal integer (lowercase letters) |
8 | %Xhexadecimal integer (UPPERcase letters) |
9 | %eexponential notation (with lowercase ‘e’) |
10 | %Eexponential notation (with UPPERcase ‘E’) |
11 | %ffloating point real number |
12 | %gthe shorter of %f and %e |
13 | %Gthe shorter of %f and %E |
"switch model: %s, %d WAN slots, IOS %f" % ("2600XM", 2, 15.4) "switch model: %s, %d WAN slots, IOS %.f" % ("2600XM", 2, 12.8) "switch model: %s, %d WAN slots, IOS %.1f" % ("2600XM", 2, 2.4) "switch model: %s, %d WAN slots, IOS %.2f" % ("2600XM", 2, 82.4)

"switch model: {}, {} WAN slots, IOS {}".format("2600XM", 20, 8.0) "model: {1}, {2} WAN slots, IOS {0}".format("2600XM", 20, 8.0)

a = "2600XM" b = 8 c = 2.0 f"model: {a}, {b} WAN slots, IOS {c}"
Strings – slicing
string1 = “The String object is used to represent and manipulate a sequence of “
string1[5:11] #slice starting at index 5 up to, but NOT including, index 11; so index 10 represents the last element in the slice

string1[5:] #slice starting at index 5 up to the end of the string
string1[:10] #slice starting at the beginning of the string up to, but NOT including, index 10
string1[:] #returns the entire string
string1[-1] #returns the last character in the string
string1[-2] #returns the second to last character in the string
string1[-9:-1] #extracts a certain substring using negative indexes
string1[-5:] #returns the last 5 characters in the string
string1[:-5] #returns the string minus its last 5 characters
string1[::2] #adds a third element called step; skips every second character of the string
string1[::-1] #returns string1's elements in reverse order