Python Strings | Strings in Python
Python Strings are characters enclosed in single quotes, double quotes or triple quotes.
Strings are sequence of characters where each character has a unique position or index no.
Strings are immutable(you can not change any character of given string).
Python Strings |
How to create strings in python ?
string1="Welcome"
or
string1=input("Enter a string")
String Indexing
Note The given string 'WELCOME' has its own unique index number. The indexes of a string begin from 0 to length-1 in forward direction and -1,-2,-3,... length in backward direction.
So if you want to retrieve any character fro the strings you can retrieve using index number.
String Slicing
In Python, String slice refers to a part of the string where strings are sliced using a range of indices.
For example :
we have a string named as S1= 'Welcome' , if we give S1[x:y] where x and y are integers indices. Python will return a slice of the string by returning the characters falling between indices x and y.
String Operators
There are two basic operators :
- Concatenation ( + ) and Replication ( * )
There are two membership operators:
- in
- not in
When we used with strings + operator performs concatenation rather than addition and * operator performs replication rather than multiplication.
Concatenation Operator
For example :
we take two strings
string1= "Hello"
string2="Python"
print(string1+string2)
Note when we use print function to display the concatenated string , it display temporary because print function is used to display text on the screen. And, Strings are immutable so we can not change it.
So if you want to change the string , you have to create a new string.
Such as
string1= "Hello"
string2="Python"
string3=string1+string2
print(string3)
Another important thing that you need to know about + operator is that this operator can work with numbers and strings separately for addition and concatenation respectively, but in the same expression, you cannot combine numbers and strings as operands with a + operator.
for example:
23 + 44=67 # addition is valid because both operands are integers.
'23' + '44'=2344 #concatenation is valid, both operands are strings.
But the expression
'23' + 4 # It is invalid. It will raise an error.
Note The + operator has to have both operands of the same type. Integer type is for addition and String type is for concatenation. It cannot work with one operand as string and one as a number.
Replication Operator
The * operator when used with numbers i.e. when both operands are numbers, it performs multiplication and returns the product of the two number operands.
To use a * operator with strings, you need two types of operands one string and another number i.e. number * string or string * number.
Where string operand tells the string to be replicated and number operand tells the number of times, it is to be repeated.
Python will create a new string that is a number of repetitions of the string operand.
for example:
"python" * 2 # it will display 'python 3 times'
python python python
Another important thing that you need to know about * operator is that this operator can work with numbers as both operands for multiplication and with a string and a number for replication respectively, but in the same expression, you cannot have strings as both the operands with a * operator.
for example:
2*3 = 6 #multiplication is valid because both operands are integers.
"2" * 3="222" # replication is valid one operand is string and other integer.
but the expression
"2" * "3" # it is invalid. It will raises an error .
Note The * operator has to either have both operands of the integer type for multiplication or one operand string type and one integer type for replication. It cannot work with the both operands of string types.
Membership Operators
There are two membership operators for strings. These are in and not in .
When we use membership operators with strings , it require that both operands used with them are of string type.
in : It returns True if a character or substring exists in the given string; false otherwise.
not in : It returns True if a character or a substring does not exist in the given string; false otherwise.
Both membership operators can work with strings variables.
String Methods
capitalize() method: It capitalize first character of the string.
replace() method: It replace the occurrence of old element to new.
upper() method: It converts lowercase characters into uppercase.
lower() method: It converts uppercase characters in to lowercase.
index() method: It returns index number.
find() method: It returns index number of the given character.
count() method: It counts total number of elements in a string.
Note Here we have used string method with print function for temporary displaying the output. It does not mean that we have made changes to a string. we cannot make changes on strings as we know that strings are immutable. So if you want to make changes to a string , you have to take another string type variable.
for example:
we have a string
str1="Programming with aarti"
str2=str1.capitalize()
print(str2)
str2=str1.find('m')
print(str2)
No comments:
Post a Comment