Python Strings
Python Strings
Example 1:
str1="This is a String"
str2='''This is a String'''
str3='This is a String'
print(str1)
print(str2)
print(str3)
Output:
This is a String
This is a String
This is a String
Example 2:
print(str1)
Output:
Error
Example 3:
print(str1)
Output:
Example 4:
str1='This is Kapil's notebook'
print(str1)
Output:
Error
Example 5:
print(str1)
Output:
Example 6:
print('''this is
a string
in seperate
lines''')
Output:
this is
a string
in seperate
lines
Example 7:
print(str)
Output:
Error
Example 8:
str='it\'s a wonderful topic'
print(str)
Output:
Example:
name = 'John'
print(message)
Output:
Hi {name}
Concatenation of string means to join (or append) one string after another. For
concatenation of string we need to define two strings and use + operator.
Example:
msg1 = 'Good'
msg3=msg1+msg2
print(msg3)
Output:
Good Morning
String Functions
Count:
Return the number of times the value "apple" appears in the string.
Example:
n=txt.count("string")
print(n)
Output:
Find:
Searches the string for a specified value and returns the position of where it
was found
Example 1:
n=txt.find("string")
print(n)
Example 2: txt="I am learning string functions. It's fun to learn about string"
n=txt.find("xyz")
print(n)
Capitalize:
The capitalize() method returns a string where the first character is upper case,
and the rest is lower case.
Example:
n=txt.capitalize()
print(n)
Output:
Example:
n=txt.title()
Output:
Lower:
Example:
n=txt.lower()
print(n)
Output:
upper:
Example:
n=txt.upper()
print(n)
Output:
Swapcase:
Swaps cases, lower case becomes upper case and vice versa
Example:
n=txt.swapcase()
print(n)
Output:
Replace:
Example:
n=txt.replace("string","python")
print(n)
Output:
Join:
Converts the elements of an iterable into a string. In simpler words, it join all
items in a tuple into a string, using a hash character as separator
Example:
n=".".join(txt)
print(n)
Output:
isspace:
txt=" "
n=txt.isspace()
print(n)
Output:
TURE
Example 2:
txt=" "
n=txt.isspace()
print(n)
Output:
FALSE
Isdigit:
Returns True if all characters in the string are digits. The isdigit() method
returns True if all the characters are digits, otherwise False.
Example 1:
txt="12345"
n=txt.isdigit()
print(n)
Output:
TRUE
Example 2:
txt="12345A"
n=txt.isdigit()
print(n)
Output:
FALSE
isaplha()
Returns True if all characters in the string are alphabets. The isaplha() method
returns True if all the characters are alphabets, otherwise False.
Example:
str1="ThisIsGood"
print(str1.isalpha())
Output:
True
Example:
x = slice(3, 5)
print(a[x])
Output:
('d', 'e')
Example 2:
s=’ HELLO ‘
print(s[3:6])
Output:
HEL
Split:
Splits the string at the specified separator, and returns a list. You can specify
the separator, default separator is white space.
Example:
print(n)
Output:
['I', 'am', 'learning', 'string', 'functions.', "It's", 'fun', 'to', 'learn', 'about', 'string']
Startswith:
Example 1:
n=txt.startswith("learning")
print(n)
Output:
False
Example 2:
n=txt.startswith("I")
print(n)
Output:
TRUE
Endwith()
Example 1:
n=txt.endswith("string")
print(n)
Output:
TRUE
Example 2:
n=txt.endswith("str")
print(n)
Output:
FALSE