Python string

A Python string is expressed with single or double quotes as follows.

s1 = 'Apple'
s2 = "Microsoft"

print(s1)  # Apple
print(s2)  # Microsoft

Length of string

The len() counts the number of letters in a string.

s1 = 'Apple'
s2 = "Microsoft"

c1 = len(s1)
c2 = len(s2)

print(c1)  # 5
print(c2)  # 9


s = 'i am aN aPPle.'

capitalize = s.capitalize()
lower = s.lower()
title = s.title()
swapcase = s.swapcase()

print(capitalize)  # I am an apple.
print(title)  # I Am An Apple.
print(swapcase)  # I AM An AppLE.
print(lower)  # i am an apple.

Check the substring exsitence

s = "Python"

if 'th' in s:

# Contained

If you want to check the specified string is contained the string, use the if-in statement.


s = 'PHP'

t = s.replace('HP', 'ython')

print(t)  # Python


Powered by Markdown