In python, string is collection of characters surrounded by single, souble or triple quotes.
Creating a string
str= "CAPSCOM TECHNOLOGY"
Printing a string
print(str)
String indexing
String indexing start from 0
Example
print(str[0]) # C
print(str[5]) # O
String splitting
Return part of a string
Example
print(str[0: 3]) # CAP
Note: 0 will be included whereas 3 will be excluded
Note: Item assignment is not possible in string
str[0]= 'T'
print(str) # it will return an error
Note: string is immutable means changing, deleting item in a string is not possible.
Deleting a string
Yes, whole string can be delete using del keyword
del str
print(str) # will throw error str is not defined.
String operators
+ | Concat a string |
* | Replicate a string |
in | it is also know as membership operators, check sub string exist in a string or not |
not in | it is also know as membership operators, check sub string not exist in a string or not |
String methods
count() | Count how many time a given character appear in a string | str="CAPSCOM" print( str.count('C') ) |
endswith() | Check string is end with given character or not | str="CAPSCOM" print( str.endswith('.') ) |
find() | Return position of first occurance of given sub string from a string, if not found return -1 | str="CAPSCOM" print( str.find('Z') ) |
format() | Format specified value in a string |
age=20 |
index() | Search a string and return index of specified sub string, and raise an exception when not found |
str="CAPSCOM" |
isalnum() | Return true if all characters of a string are alphanumeric |
|
isalpha() | Return true if all characters of a string are alphabet |
|
isdigit() | Return true if all characters of a string are digits |
|
islower() | Return true if all characters of a string in lower case |
|
isupper() | Return true if all characters of a string in upper case |
|
isspace() | Return true if all characters of a string are whitespace |
|
lstrip() | Return a string after removing white space from left side of a string |
|
rstrip() | Return a string after removing white space from right side of a string |
|
lower() | Return lowercase string |
|
upper() | Return uppercase string |
|
startswith() | Return true if string start with given sub string |
|
replace() | Return new string by replacing a substring at specified string |
|
join() |
Takes items from iterable and join them together in a string with specified separator. |
t = ("Computer", "UPS", "CPU") |