String Operations
In general, you can’t perform mathematical operations on strings, even if the strings look like numbers.
The + operator performs strings concatenation, which means it joins the strings by linking them end-to-end.
Example:
>>> first="First"
>>> second="Second"
>>> fullstr=first+""+second
>>> print(fullstr)
FirstSecond
>>> print('Spam'*3)
SpamSpamSpam
>>> print("M"*5)
MMMMM
What is String ?
- String is collection of characters, these characters can be alphabets(a-z, A-Z), digits(0-9) and special characters.
- String allows characters available in other languages.
- Python allows to represent string in two formats.
1. Ascii – This string allows only alphabets of English.
2. Unicode – This string allows characters form other languages.
Datatypes:
1. Standard Datatypes:
int
float
complex
boolean
2. Sequence Datatypes:
str
list
tuple
set
- String is a sequence datatypes.
- str is class name, which is used in python to create string objects.
* Special characters in Strings:
In python the backslash(\) character is used to introduce a special character.
Escape Sequence Meaning
\n Newline
\t Horizontal Tab
\\ Backslash
\’ Single Quote
\” Double Quote
Example:
>>> print("This is Back Slash(\\)Mark")
This is Back Slash(\)Mark
>>> print("This is Back Slash \t Mark.")
This is Back Slash Mark.
>>> print("This is Back Slash\n Mark.")
This is Back Slash
Mark.
>>> print("This is Back Slash \' Single Quatrs \' Marks")
This is Back Slash ' Single Quatrs ' Marks
>>> print("This is \\ slash")
This is \ slash
Define String Slicing?
To cut a substring from a string is called string slicing.
Slicing is a process of dividing a sequence into sub-sequence.
String slicing is dividing string into substring.
Slicing is done in two ways :
1. Using Slice Operator.
2. Using Slice Method.
1. Slice Operator :
Syntax:
[start:stop:step]
[start:]
[:stop]
[::]
Start: starting index and default is 0.
Stop: ending index and default is length.
Step: Incremental or decremental value and default is +1.
Example:
>>> s='PYTHON'
>>> s[:]
'PYTHON'
>>> s[:3]
'PYT'
>>> s[::]
'PYTHON'
>>> s[::3]
'PH'
>>> s[:3]
'PYT'
>>> s[4::]
'ON'
>>> s[3::]
'HON'
>>> s[-1:-6]
''
>>> s[-1:-6:-1]
'NOHTY'
>>> s[-1::-1]
'NOHTYP'
>>> s[-1::1]
'N'
>>> s[-6:-1]
'PYTHO'
>>> s[-6]
'P'
>>> s[0:100]
'PYTHON'
>>> s[0:7]
'PYTHON'
>>> s[9:10]
''
>>> s1=s[-100:-1]
>>> print(s1)
PYTHO
* String Indices or Indexing:
String are arrays of characters and elements of an array can be accessed using indexing. Indices start with 0 from left side and -1 when from right side.
Here two indices are used separated by a colon (:). A slice 3:7 means indices characters of 3rd,4th,5th and 6th positions. The second integer index i.e. 7 is not included. You can use negative indices for slicing.
Each character inside string is identified with unique integer number, which is called index.
Index number can be +ve & -ve.
Positive index is given from 0 to length -1 it allows to read characters from to right.
Negative index is given from -1 to length, it allows to read characters from right to left.
Index allows to read characters from string in seq order or random order.
Each character read from string is again string type.
string_name[index]
If index is invalid, python runtime gives Index_Error.
Example:
s=”PYTHON”
s
P Y T H O N
Example:
>>> str="Naresh i Technologies"
>>> print(str[0])
N
>>> print(str[-1])
s
>>> print(str[1:4])
are
>>> print(str[:4])
Nare
* How to create string objects?
- Python allows to create string object in two ways.
1. Implicit Creation.
2. Explicit Creation.
1. Implicit Creation:
- This objects is created by python runtime.
- Anything in python represent within
1. ‘ ’
2. “ ”
3. “’ ‘”
is an instance of str class.
- “’ ‘” allows to represent string in multiple line.
Note :
String is immutable(do not changes) after creating string object. We connot do any changes.
An awesome blog for the freshers. Thanks for posting this information.
ReplyDeletePython Online Training in Hyderabad
Python Training in Hyderabad
Python Training
Python Online Training