Python strings
Python string is a built-in type text sequence. It is used to handle textual data in python. Python Strings are immutable sequences of Unicode points. Creating Strings are simplest and easy to use in Python.
We can simply create Python String by enclosing a text in single as well as double quotes. Python treat both single and double quotes statements same.
Example :
a = "Hello"
b= 'ApkZube'
print(a+" "+b)
output of above example :
Hello ApkZube
advertisement
Accessing Python Strings
advertisement
- In Python, Strings are stored as individual characters in a contiguous memory location.
- The benefit of using String is that it can be accessed from both the directions (forward and backward).
- Both forward as well as backward indexing are provided using Strings in Python.
- Forward indexing starts with 0,1,2,3,....
- Backward indexing starts with -1,-2,-3,-4,....
str[0]='P'=str[-6] ,
str[1]='Y' = str[-5] ,
str[2] = 'T' = str[-4] ,
str[3] = 'H' = str[-3] ,
str[4] = 'O' = str[-2] ,
str[5] = 'N' = str[-1].
advertisement
Python String Example
Here, we are creating a simple program to retrieve String in reverse as well as normal form.
name="Rajat"
length=len(name)
i=0
for n in range(-1,(-length-1),-1):
print(name[i],"\t",name[n])
i+=1
output :
R t
a a
j j
a a
t R
advertisement
Python Strings Operators
To perform operation on string, Python provides basically 3 types of Operators that are given below.
- Basic Operators.
- Membership Operators.
- Relational Operators.
advertisement
1. Basic Operators
There are two types of basic operators in String + and *.
String Concatenation Operator (+)
str1="apk"
str2="Zube"
print(str1+str2)
Output :
apkZube
String Concatenation Operator (+)
Expression | Output |
---|---|
"10"+"50" | "1050" |
"apk"+"007" | "apk007" |
"apk97"+"zube97" | "apk97zube97" |
NOTE: Both the operands passed for concatenation must be of same type, else it will show an error.
print("apkZube"+97)
output :
Traceback (most recent call last):
File "main.py", line 1, in
print("apkZube"+97)
TypeError: Can't convert 'int' object to str implicitly
advertisement
Python String Replication Operator (*)
Replication operator uses two parameters for operation, One is the integer value and the other one is the String argument.
The Replication operator is used to repeat a string number of times. The string will be repeated the number of times which is given by the integer value.
For Example :
print("ApkZube" * 5)
print(3 * "Python")
output :
ApkZubeApkZubeApkZubeApkZubeApkZube
PythonPythonPython
Python String Replication Operator (*)
Expression | Output |
---|---|
"ArcX" * 2 | "ArcXArcX" |
3 * '5' | '555' |
'@' * 5 | '@@@@@' |
NOTE: We can use Replication operator in any way i.e., int * string or string * int. Both the parameters passed cannot be of same type.
advertisement
2 . Python String Membership Operators
Membership Operators are already discussed in the Operators section. Let see with context of String.
There are two types of Membership operators :
- in - "in" operator returns true if a character or the entire substring is present in the specified string, otherwise false.
- not in - "not in" operator returns true if a character or entire substring does not exist in the specified string, otherwise false.
lets see the example :
str1="ApkZube"
str2="Apk"
str3="Zube"
str4="Dev"
print('Exmple of in operator ::')
print(str2 in str1)
print(str3 in str1)
print(str4 in str1)
print()
print(str2 not in str1)
print(str3 not in str1)
print(str4 not in str1)
output :
Exmple of in operator ::
True
True
False
False
False
True
advertisement
3. Python Relational Operators
All the comparison (relational) operators i.e., (<,><=,>=,==,!=,<>) are also applicable for strings. The Strings are compared based on the ASCII value or Unicode(i.e., dictionary Order).
Example :
print("ApkZube"=="ApkZube")
print("apkZube">="ApkZube")
print("A"<"a")
output :
True
True
True
Explanation:
The ASCII value of a is 97, b is 98, c is 99 and so on. The ASCII value of A is 65, B is 66, C is 67 and so on. The comparison between strings are done on the basis on ASCII value.
advertisement
Python String Slice Notation
Python String slice can be defined as a substring which is the part of the string. Therefore further substring can be obtained from a string.
There can be many forms to slice a string, as string can be accessed or indexed from both the direction and hence string can also be sliced from both the directions.
Syntax of Slice Operator :
str[start : stop : step ]
other syntax of slice
str[start : stop] #items start through stop-1
str[start : ] #items start through the rest of the array
str[ : stop] #items from the beginning through stop-1
str[ : ] # a copy of the whole array
example :
s="Monty Python"
print(s[6:10])
print(s[-12:-7])
print(s[-1: :-1]) #reversed all string
print(s[2: 10: 2]) #step = 2
print(s[ : : -1]) #reversed all string
print(s[ : 5]) #from 0 to 4
print(s[3 : ]) #from 3 to end of the string
print(s[ : ]) #copy all strin
output :
Pyth
Monty
nohtyP ytnoM
nyPt
nohtyP ytnoM
Monty
ty Python
Monty Python
NOTE: Both the operands passed for concatenation must be of same type, else it will show an error.
advertisement