String Manipulation - Python
Traversing a string
Traversing refers to iterating through the elements of a string, one character at a time.
name = "python"
for ch in name:
print(ch , '-', end='')
The above code will print:
p-y-t-h-o-n-
String Operators
1. Basic Operators
String Concatenation Operator +
The + operator creates a new string by joining the two operand strings.
For example:
"tea"+"pot"
will result into
'teapot'Note:
The + operator has to have both operands of the same type either of number types (for addition) or of string types (for concatenation). It cannot work with one operand as string and one as number.
String Replication Operator *
The * operator performs multiplication and returns the product of the two number operands.
For example:
3*"code!"
will return
'code!code!code!'
Note:
The + operator has to have both operands of the same type either of number types (for multiplication) or one string type and one number type (for replication). It cannot work with both operands of string types.
in
Returns True if a character or a substring exists in the given string ; False otherwise.
For example;
"e" in "Hello" wil give True
"it" in "Hello" will give False
not in
Return True if a character or a substring doesn't exist in the given string. False otherwise.
For example:
"123" not in "Hello" wil give True
"in" not in "India" will give False
3. Comparison Operators
"a" == "a" True
"a" != "A" True
"ABC" == "abc" True
String Slices
It refers to a prat of the string, where strings are sliced using a range of indices.
For example:
Let we have a string namely word storing a string 'WONDERFUL'.
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---|---|---|---|---|---|---|---|---|
W | O | N | D | E | R | F | U | L |
-9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
word [0:7] will give 'WONDERF'
word [2:5] will give 'NDE'
word [-7:-1] will give 'NDERFU'
word [::-1] will give 'LUFREDNOW'
String functions and methods
Python also offers many built-in functions and methods for string manipulation.
1. string.capitalize()
It returns a copy of the string with its character capitalized.
For example:
>>>true.capitalize()
True
2. string.find(sub[,start [,end]])
True
Returns the lowest index in the string where the substring sub is found within the slice range of start and end. It returns 1 if sub is not found.
For example:
>>>string = it goes as - ringa ringa roses
>>>sub = ringa
>>>string.find(sub)
13
>>>sub = ringa
>>>string.find(sub)
13
3. string.isalnum()
It returns True if the characters in the string are alphanumeric(alphabets or numbers) and there is at least one character, False otherwise.
For example:
>>>string1 = "abc123"
>>>string2 = " "
>>>string1.isalnum()
True
>>>string2.isalnum()
False
>>>string2 = " "
>>>string1.isalnum()
True
>>>string2.isalnum()
False
4. string.isalpha()
Returns True if all characters in the string are alphabetic and there is at least one character, False otherwise.
For example:
5. string.isdigit()Returns True if all characters in the string are alphabetic and there is at least one character, False otherwise.
For example:
>>>string = "abc123"
>>>string2="hello"
>>>string.isalpha()
False
>>>string2.isalpha()
True
>>>string2="hello"
>>>string.isalpha()
False
>>>string2.isalpha()
True
Returns True if all the characters in the string are digits. There must be at least one character, False otherwise.
For example:
>>>string = "abc123"
>>>string2="12345"
>>>string.isdigit()
False
>>>string2.isdigit()
True
>>>string2="12345"
>>>string.isdigit()
False
>>>string2.isdigit()
True
6. string.islower()
Returns True if all cased characters in the string are lowercase. There must be at least one cased character. It returns False otherwise.
For example:
>>>string = "hello"
>>>string2 = "THERE"
>>>string2.islower()
False
>>>string.islower()
True
>>>string2 = "THERE"
>>>string2.islower()
False
>>>string.islower()
True
7. string.isspace()
Returns True if there are only whitespace characters in the string. There must be at least one character. It returns False otherwise.
For example:
>>>string = " "
>>>string2 = ""
>>>string.isspace()
True
>>>string2.isspace()
False
>>>string2 = ""
>>>string.isspace()
True
>>>string2.isspace()
False
8. string.isupper()
Tests whether all cased characters in the string are uppercase and requires that there be at least one cased character. Return True if so and False otherwise.
For example:
>>>string = "There"
>>>string2 = "HELLO"
>>>string.isupper()
False
>>>string2.isupper()
True
>>>string2 = "HELLO"
>>>string.isupper()
False
>>>string2.isupper()
True
9. string.lower()
Returns a copy of the string converted to lowercase.
For example:
>>>string = "Hello"
>>>string.lower()
>>>'hello'
>>>string.lower()
>>>'hello'
10. string.upper()
Returns a copy of the string converted to uppercase.
For example:
>>>string = "hello"
>>>string.upper()
>>>'HELLO'
>>>string.upper()
>>>'HELLO'
No comments: