Data Structures and Algorithms-1
Data Structures and Algorithms-1
LECTURER : MR RUWA
GROUP 22
A string is an array of characters, either as a literal constant or as some kind of variable. The
latter may allow its elements to be mutated and the length changed and it may be fixed (after
creation). A string is generally considered as a data type and is often implemented as an array
data structure of bytes (or words) that stores a sequence of elements, typically characters,
using some character encoding. String may also denote more general arrays or other sequence
(or list) data types and structures.
Depending on the programming language and precise data type used, a variable declared to
be a string may either cause storage in memory to be statically allocated for a predetermined
maximum length or employ dynamic allocation to allow it to hold a variable number of
elements. When a string appears literally in source code, it is known as a string literal or an
anonymous string.
When working with strings there two tools which are very important which are String
processing and String manipulation. When you want to search for a certain expression in a
string, add multiple strings one has to be familiar with the concepts of string processing.
String processing is the Data Manipulation refers to the process of changing and organising
information to make it more organised and readable.
1. Stripping Whitespace
Stripping whitespace is an elementary string processing requirement. You can strip leading
whitespace with the lstrip() function (starting from the left side) and you can string the
trailing whitespace with the rstrip() function (starting from the right side) and to strip both
the leading and the trailing strip the strip() function is used.
Diagram representation of string strip() method functionality in python.
2. Splitting Strings
Splitting strings into lists of smaller substrings is often useful and easily
accomplished in Python with the split() function
2. Joining List Elements Into a String
You can join list element strings into a single string in Python using the
join(.) function
(‘ ’.join(s))
4. Reversing a String
Python does not have a built-in string reverse method. However, given that
strings can be sliced like lists, reversing one can be done in the same succinct
fashion that a list's elements can be reversed.
PYTHON
.format (s[;;-1]))
NOHTYP
6. Replacing Substrings
To replace substrings, use the Python replace() string function