Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

4.

0 STRINGS
4.1 STRING OPERATIONS
4.1.1 Concatenation
Strings can be concatenated by using the plus (+) sign. Concatenation means to add a String at the
end of the other String.
4.1.2 Repetition
Strings can be repeated by using the star (*) sign. Repetition means to print the String several times.
4.2 CHARACTER ENCODING
in character encoding, each character is given a unique number or a code, so it can be stored in a
computer. UTF-8 is the main character encoding for the web and simple English text. In UTF-8,
UTF stands for Unicode Transformation Format and 8 stands for 8-bit values that are used for
4.3 STRING FUNCTIONS AND METHODS
Functions
When a function is applied to data, it creates new data and returns the produced result.
4.3.1 len()
Returns the length of the String. len(str).
4.3.2 del()
Deletes the entire String. del str.
4.3.3 max()
Returns the maximum of two Strings based on the UTF-8 encoding. max(str1, str2).
4.3.4 min()
Returns the minimum of two strings. max(str1, str2).

Methods
Methods are like functions but they differ in their invocation style.
4.3.5 capitalize()
Converts the first character of a String to uppercase. str.capitalize().
4.3.6 lower()
Converts all characters of a String to lowercase. str.lower()
4.3.7 upper()
Converts all characters of a String to uppercase. str.upper().
4.3.8 title()
Converts the first character of each word of a String to its upper case. str.title().
4.3.9 replace()
Replaces old substrings with a new substring.
str.replace(old, new,[count]).
If the third parameter is absent all the old substrings are replaced. And if the third parameter is
present, that is an Integer, replaces that many occurrences form the left.
4.3.10 split()
Splits the String into a maximal number of words.
str.split([chars]). It may take an optional parameter. If no parameter is passed, It takes the word
separator as whitespace by default.
4.3.11 strip()
Removes the leading and trailing characters from a String that is passed as an argument.
str.strip([chars]). If the parameter is omitted or if the parameter is None, it removes whitespace.
4.2.3.12 count()
This method returns the number of non-overlapping occurrences of a substring in a given String.
The general syntax is: str.count(sub[, start[, end]]). It takes three parameters. The first parameter is
compulsory, it is the substring that we are search- ing for. The parameter “start” and “end” are
optional. If “start” is missing, it is treated as the beginning of the String, if the “end” is missing it is
treated as the length of the String.
4.3.13 isalpha()
Returns True if all characters in the String are alphabetic characters. str.isalpha().
4.3.14 isupper()
Returns True if all characters in the String are in uppercase. str.isupper().
4.4 STRING SLICING
Slicing allows us build a new String out of an existing one.
Syntax: str2=str1[start : end : step]
4.4.1 Slicing with Negative Index
Negative indexes and mixed indexes can also be used to slice a list.
4.2.4.2 String Slicing with Step
Slicing can be done with a step size also.
4.2.4.3 String Slicing Default Index
While slicing a String, the parameters are start, end and step values, separated by a colon. If one or
more parameters are missing, Python uses the default values. Which are 0, last, and 1.

You might also like