STRING
STRING
For example,
Place = "Hathras"
Traversing A String
Traversing a string means accessing all the elements of the string one after other by
using the subscript or index value.
For example,
String = "Information"
I n f o r m a t i o n
0 1 2 3 4 5 6 7 8 9 10
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Each character in a string has an index value, indexing starts at 0 and must be an
Integer. Individual elements can be accessed by typing index value inside [ ] square
brackets.
For example,
print (string [2]) = f
Iteration through string
for i in string:
print (I,"end=",") = I,n,f,o,r,m,a,t,i,o,n
Comparison Operators
We can use relational / comparison Operators (>,<,>=,<=,==,!=) to compare two
strings.
Str1 = "Hello"
Str1 = "hello"
print (Str1 == Str2) = False
String slicing
Slicing is used to retrieve a subset of values. A slice of a string is nothing but a
substring.
Syntax:
String_name[start:end]
2. capitalise() - This method returns the exact copy of the string with the first letter
in uppercase.
3. split() - The split method breaks up a string at the specified separator and
returns a list substring.
5. isalpha() - Returns True if all the characters in the string are alphabetic.
6. isdigit() - Returns True if all the characters in the string are digits.
7. isspace() - Returns True if there are only whitespace characters in the string.
8. islower() - Returns True if all cased characters in the string are lowercase.
9. isupper() - Returns True if all cased characters in the string are Uppercase.
10. lower() - Returns a copy of string converted to lowercase.