Storing multiple values of same or different types under a single name. They are collection of data.
Strings are ordered collection or an array of characters.
Represented by quotes either ‘ ‘ or ” “.
They are generally immutable. This means individual elements can’t be changed.
Example: 'Hello', "Python"
order of elements: index of element
Index - may be positive or negative
Order (element) | First | Second | Third | … | Last |
---|---|---|---|---|---|
Positive Index | 0 | 1 | 2 | … | n-1 |
Negative Index | -n | -n+1 | -n+2 | … | -1 |
str = 'String'
Syntax:
str(value = None)
IN [2]
str1 = 'Hello'
type(str1)
IN [3]
s = str([10, 20])
print(s)
type(s)
stdout
[10, 20]
All the values that are taken using input()
method as strings.
IN [8]
s1 = input()
type(s1)
# Input: String
stdin
String
Two strings can be concatenated by using +
operator.
IN [11]
str1 = 'String '
str2 = 'Concatenation'
str1+str2 # Concatenation
A character can be appended to string using +=
operator or by reassignment.
IN [14]
str3 = 'Let'
ch = 's'
str3 += ch # str3 = str3 + ch
str3
String multiplication results a string with repitition. It can be done using *
operator.
IN [16]
str4 = 'Namaste '
str4*4
There are various methods to work with strings. But Unlike lists, string methods don’t change strings as ther are immutable. Hence the returned value should always be reassigned.
Methods: 1. capitalize 2. upper 3. lower 4. Center 5. swapcase 6. find 7. rfind 8. index 9. startswith 10. endswith 11. isalnum 12. isalpha 13. isdigit 14. strip 15. split 16. join 17. replace 18. zfill 19. enumerate 20. len 21. isupper 22. islower 23. lstrip 24. rstrip 25. partition 26. ljust 27. rjust 28. sorted
Capitalize the string. More specifically, first letter of the string in upper case and rest in lower case.
Syntax:
str.capitalize()
IN [18]
s1 = 'hello'
s1 = s1.capitalize()
s1
Converts the string to upper case
Syntax:
str.upper()
IN [19]
s1 = 'hello'
s1 = s1.upper()
s1
Converts the string to lowercase
Syntax:
str.lower()
IN [20]
s1 = 'HI'
s1.lower()
Returns original string to center of given width. The empty places are filled using fillchar
Syntax:
str.center(width, fillchar)
IN [22]
s1 = 'hii'
s1 = s1.center(5, '*')
s1
IN [23]
s2 = 'Well'
s2 = s2.center(10, '-')
s2
Swaps the case of string to lower if in uppercase, to upper in lowercase
Syntax:
str.swapcase()
IN [24]
s1 = 'PyThon'
s1.swapcase()
Returns the index of start of substring if present in string else -1.
Syntax:
str.find(substring, start = 0, end = length)
IN [32]
s1 = 'Python'
s1.find('Py')
Returns index of start of last occurence substring if present in string else -1
Syntax:
str.rfind(substring, start = 0, end = length)
IN [34]
s1 = 'Hello Welcome'
s1.rfind('el')
Returns the index of substring given. Same as find but raises error if absent.
Syntax:
str.index(substring, start = 0, end = length)
IN [37]
s1 = 'Python'
s1.index('Py')
Checks if the given string starts with given Prefix.
Syntax:
str.startswith(prefix, start = 0, end = length)
IN [6]
str1 = 'preprocessing'
str1.startswith('pre')
Checks if the given string ends with the given suffix.
Syntax:
str.endswith(suffix, start = 0, end = length)
IN [7]
str1 = 'preprocessor'
str1.endswith('or')
Checks if the given string is alpha-numeric, i.e., contains only alphabets or numbers.
Syntax:
str.isalnum()
IN [8]
s2 = '$hello_'
s2.isalnum()
IN [11]
s3 = 'dev23'
s3.isalnum()
Checks if the given string is alphabetic, i.e., contains only alphabets.
Syntax:
str.isalpha()
IN [12]
s3 = 'dev23'
s3.isalpha()
IN [13]
s4 = 'python'
s4.isalpha()
Checks if the given string is numeric, i.e., contains only numbers.
Syntax:
str.isdigit()
IN [14]
s3 = 'dev23'
s3.isdigit()
IN [15]
s5 = '123'
s5.isdigit()
Removes the leading and trailing whitespaces by default, if any. If chars is given (string only), it removes only the characters in it.
Syntax:
str.strip(chars = None)
IN [16]
str1 = ' Hello '
str1.strip()
IN [21]
str2 = 'HhelloH '
str2.strip('H ')
Return a list of the words in the string, using sep
as the delimiter. maxsplit
gives the Maximum no. of splits to be done. The remaining string returned as last element. By default whitespaces, line feed and carriage returns are taken as delimiters.
Syntax:
str.split(/, sep = None, maxsplit = -1)
IN [22]
str1 = 'Hello\n World\r'
str1.split()
IN [27]
str2 = 'Hello-World '
str2.split('-')
IN [32]
str1 = 'Hello\n World\r'
str1.split(maxsplit = 1) # Limiting no. of split
Returns a concatenated string of iterable (containing only strings) with char (string) as delimiter.
Syntax:
char.join(iterable)
IN [29]
list1 = ['Hello', 'World']
"".join(list1)
IN [30]
list1 = ['Hello', 'World']
" ".join(list1)
IN [31]
list1 = ['Hello', 'World']
"-".join(list1)
Returns a copy of a string with given substring replaced by old substring. count is the max no. of occurences to be replaced, all be default.
Syntax:
str.replace(old_string, new_string, count = -1)
IN [35]
str1 = 'Hello World'
str1.replace('l','L')
Pads zeroes (0) to the start of the numeric string to fill the given width if width > length of string.
Syntax:
str.zfill(width)
IN [36]
num = '123'
num.zfill(2)
IN [37]
num = '123'
num.zfill(5)
Returns the enumeration object of given string.
Syntax:
enumerate(str)
IN [44]
str1 = 'Hello'
print('(Index, Value)')
print(*enumerate(str1), sep='\n') # enumeration object
stdout
(Index, Value)
(0, 'H')
(1, 'e')
(2, 'l')
(3, 'l')
(4, 'o')
Returns the length o given string
Syntax:
len(str)
IN [42]
str1 = 'Prabhu'
len(str1)
Checks if the given string is in uppercase.
Syntax:
str.isupper()
IN [45]
str1 = 'HYMN'
str1.isupper()
Checks if the given string is in lowercase.
Syntax:
str.islower()
IN [46]
str2 = 'welcome'
str2.islower()
Removes the leading whitespaces by default, if any. If chars is given (string only), it removes only the characters in it.
Syntax:
str.lstrip(chars = None)
IN [47]
str1 = ' Hello '
str1.lstrip()
Removes the trailing whitespaces by default, if any. If chars is given (string only), it removes only the characters in it.
Syntax:
str.rstrip(chars = None)
IN [48]
str1 = ' Hello '
str1.rstrip()
Partition the string into three parts using the given separator.
If the separator is found, returns a 3-tuple containing the part before the separator, the separator itself, and the part after it.
If the separator is not found, returns a 3-tuple containing the original string and two empty strings.
Syntax:
str.partition(sep)
IN [51]
str2 = 'Hello-World'
str2.partition('-')
IN [54]
str2 = 'Hello-World'
str2.partition('a')
Pads given char (default - space) to the end of the string to fill the given width if width > length of string. Left justified string.
Syntax:
str.ljust(width, fillchar = ' ')
IN [55]
s1 = 'hii'
s1.ljust(5, '*')
Pads given char (default - space) to the start of the string to fill the given width if width > length of string. Right justified string.
Syntax:
str.rjust(width, fillchar = ' ')
IN [57]
s1 = 'hii'
s1.rjust(5)
Returns sorted string as a list based on given key.
Syntax:
sorted(iterable, key = None, reverse = False)
IN [58]
s1 = 'hii'
sorted(s1)
Elements of a string can be accessed using index.
Example:
Consider a string,
s = 'Hello'
Length = 5
Element | ‘H’ | ‘e’ | ‘l’ | ‘l’ | ‘o’ |
---|---|---|---|---|---|
Position | 1 | 2 | 3 | 4 | 5 |
Positive Index | 0 | 1 | 2 | 3 | 4 |
Negative Index | -5 | -4 | -3 | -2 | -1 |
Calling $i^{th}$ element:
positive index: s [i-1]
negative index: s [i - 1 - length]
IN [1]
s1 = 'Hello'
print(s1[0])
print(s1[-2])
stdout
H
l
Used to get a substring of a string. Denoted by []
Syntax:
string_name[start = 0 : stop = length : stride = 1]
IN [60]
s1 = 'Python is Easy to Learn'
s1[5:-3]
Use for or while loop to access element or string.
Using *
will convert the characters in the string into arguements of print method
IN [2]
s2 = 'Welcome'
for i in s2:
print(i, end='')
stdout
Welcome
IN [1]
s3 = 'Python Programming'
for i in range(len(s3)):
print(s3[i], end='')
stdout
Python Programming
IN [61]
s2 = 'Book'
print(*s2)
stdout
B o o k
Two built in methods, ord and chr can be used to work with ASCII values.
Returns the decimal ASCII (ordinal) value or Unicode point of given one-character string.
Syntax:
ord(c)
Returns the one character Unicode or ASCII string of given ordinal.
Syntax:
chr(i)
IN [1]
s = 'a'
ord(s)
IN [2]
o = 97
chr(o)
Python allows usage of relational operators on strings.
Python compares two strings using Lexicographical order (dictionary arrangement), i.e. using ASCII values of these characters.
This means that ‘B’(ASCII value = 66) is smaller than ‘b’ (ASCII value = 98)
IN [3]
sample1 = 'Python'
sample2 = 'midhun'
sample1 > sample2
IN [5]
sample1 = 'PYTHON'
sample2 = 'pYTHON'
sample1 < sample2
IN [1]
sample1 = 'Hi'
sample2 = 'Hello'
sample1 > sample2
Returns the maximum value in the given string. Returns the character with maximum ASCII value in the String by default.
Syntax:
max(String, key = None)
IN [9]
s = 'hello'
max(s)
IN [10]
s = 'hello'
max(s, key = lambda x: s.count(x))
Returns minimum value in the string. Returns the character with minimum ASCII value in the String by default.
Syntax:
min(iterable, key = None)
IN [13]
s = 'hello'
min(s)
Returns iterator object of reversed string.
Syntax:
revrsed(sequence)
IN [14]
s = 'hello'
for i in reversed(s):
print(i, end='')
stdout
olleh
Returns a list of strings split with carriage return and line feed (CRLF) as separator.
Syntax:
str.splitlines(keepends = False)
IN [19]
s1 = 'Hello\n\rPython'
s1.splitlines()
IN [18]
s1 = 'Hello\n\rPython'
s1.splitlines(keepends = True)
Returns formatted string as required.
Syntax:
str.format(*args, **kwargs)
It was already discussed at String formatting in Introduction to Python Programming
Refer discussion in Introduction to Python Programming