- How would you confirm that 2 strings have the same identity/location?
a = 289
b = a
print(a == b) #=> True
print(a is b) #=> True
- How would you check if each word in a string begins with a capital letter?
a= "This Is Worst Example"
print(a.istitle())
OUTPUT: True
- Check if a string contains a specific substring
s = "stackorigin is a Q&A website"
print(s.find('is'))
OUTPUT: 12
s = "stackorigin is a Q&A website"
print(s.find('Is'))
OUTPUT: -1
If sub string not found output is: -1
s = "stackorigin is a Q&A website"
sub = "website"
if sub in s:
print("{} sub string is exist in main string". format(sub))
OUTPUT: website sub string is exist in main string
s = "stackorigin is a Q&A website is"
print(s.index("is"))
OUTPUT: 12
s = "stackorigin is a Q&A website is"
print(s.rindex("is"))
OUTPUT: 29
Difference of find() and index():
- find() returns -1 if the substring is not found.
- index() will throw a ValueError.
- Count the total number of characters in a string
s = "stackorigin is a Q&A website"
print(len(s))
OUTPUT: 28
- Count the number of a specific character in a string
s = "stackorigin is a Q&A website"
print(s.count('is'))
OUTPUT: 1
- Make Capitalize the first character of a string
s = "stackorigin is a Q&A website"
print(s.capitalize())
OUTPUT: Stackorigin is a q&a website
- F-Strings in Python
s = "stackorigin is a Q&A website"
print("hello {}".format(s))
print(f'hello {s}') => #Only available after python 3.6 version
- Check if a string contains only numbers
s = "stackorigin is a Q&A website"
print(s.isnumeric())
OUTPUT: False
s = "33232.3223"
print(s.isnumeric())
OUTPUT: False
s = "33232"
print(s.isnumeric())
OUTPUT: True
- split a string by a character: Split a string into a list
data = 'this, is, best, example'
print(data.split(', '))
OUTPUT: ['this', 'is', 'best', 'example']
data = 'this, is, best, example'
print(data.split(', ', 2))
OUTPUT: ['this', 'is', 'best, example']
- check if a string contains only lower case letters in Python
data = 'this is best example'
print(data.islower())
OUTPUT: True
- Python Program to Reverse a given String
def reverse(str):
s = ""
for ch in str:
s = ch + s
return s
mystr = "stackorigin is a Q&A website"
print("Reversed String: ", reverse(mystr))
OUTPUT: Reversed String: etisbew A&Q a si nigirokcat
- Uppercase first and last character of a given string
s = "stackorigin is a Q&A website"
print(s[0].upper()+s[1:-1]+s[-1].upper())
OUTPUT: Stackorigin is a Q&A websitE
- Python String splitlines() Method: splitlines() splits a string on Representation
Representation | Description |
---|---|
\n | Line Feed |
\r | Carriage Return |
\r\n | Carriage Return + Line Feed |
\x1c | File Separator |
s = "stack\norigin is a Q\r&A website"
print(s.splitlines())
OUTPUT: ['stack', 'origin is a Q', '&A website']
- Python Trim String – rstrip(), lstrip(), strip()
string = ' stackorigin is a Q&A website '
print(string.lstrip())
print(string.rstrip())
print(string.strip())
OUTPUT:
stackorigin is a Q&A website
stackorigin is a Q&A website
stackorigin is a Q&A website
- Check whether a string starts with and ends with
string = 'stackorigin is a Q&A website'
print(string.startswith('stackorigin'))
OUTPUT: True
string = 'stackorigin is a Q&A website'
print(string.endswith('website'))
OUTPUT: True
Leave a comment