Functions String (python)

length of a string , min and max

>>> s = "Welcome"
>>>len(s)
7
>>> max(s)
 'o'
>>> min(s)
'W'

 

Index operator []

s[index]

>>> s = "Welcome"
>>> for i in range(0, len(s), 2):
... print(s[i], end = '')
Wloe
>>>
1 >>> s = "Welcome"
2 >>> s[-1]
3 'e'
4 >>> s[-2]
5 'm'
6 >>>

 

Slicing Operator

The slicing operator returns a slice of the string using the syntax s[start : end]. The slice
is a substring from index start to index end – 1.

 s = "Welcome"
 s[1 : 4]
3 'elc'
>>> s = "Welcome"
 >>> s[ : 6]
 'Welcom'
 >>> s[4 : ]
 'ome'
 >>> s[1 : -1]
 'elcom'
 >>>

 

The Concatenation (+) and Repetition (*) Operators

You can join, or concatenate, two strings by using the concatenation operator (+). You can
also use the repetition operator (*) to concatenate the same string multiple times.

 

>>> s1 = "Welcome"
 >>> s2 = "Python"
 >>> s3 = s1 + " to " + s2
 >>> s3
 'Welcome to Python'
 >>> s4 = 3 * s1
>>> s4
 'WelcomeWelcomeWelcome'
 >>> s5 = s1 * 3
 >>> s5
 'WelcomeWelcomeWelcome'

The in and not in Operators

>>> s1 = "Welcome"
>>> "come" in s1
True
>>> "come" not in s1
False
>>>
s = input("Enter a string: ")
if "Python" in s:
    print("Python", "is in", s)
else:
    print("Python", "is not in", s)

 

Comparing Strings

You can compare strings by using the comparison operators (==,!=, >, >=, <, and <=)

 

>>> "green" == "glow"
False
>>> "green" != "glow"
True
>>> "green" > "glow"
True
>>> "green" >= "glow"
True
>>> "green" < "glow"
False
>>> "green" <= "glow"
False
>>> "ab" <= "abc"
True
s1 = input("Enter the first string: ")
s2 = input("Enter the second string: ")
if s2 < s1:
    s1, s2 = s2, s1

print("The two strings are in this order:"

 

Iterating a String

for ch in s:
    print(ch)
for i in range(0, len(s), 2):
    print(s[i])

 

Some more String functions

isalnum(): bool

Returns True if characters in this string are alphanumeric

and there is at least one character.

isalpha(): bool

Returns True if characters in this string are alphabetic

and there is at least one character.

isdigit(): bool

Returns True if this string contains only number characters.

isidentifier(): bool

Returns True if this string is a Python identifier.

islower(): bool

Returns True if all characters in this string are lowercase

lettters and there is at least one character.

isupper(): bool

Returns True if all characters in this string are uppercase

lettters and there is at least one character.

isspace(): bool

Returns True if this string contains only whitespace characters.

 

>>> s = "welcome to python"
 >>> s.isalnum()
 False
>>> "Welcome".isalpha()
 True
>>> "2012".isdigit()
True
 >>> "first Number".isidentifier()
False
>>> s.islower()
True
>>> s.isupper()
 False
>>> s.isspace()
False
>>>

 

Searching for Substrings

endswith(s1: str): bool

Returns True if the string ends with the substring s1.

startswith(s1: str): bool

Returns True if the string starts with the substring s1.

find(s1): int

Returns the lowest index where s1 starts in this string, or –1 if

s1 is not found in this string.

rfind(s1): int

Returns the highest index where s1 starts in this string, or –1 if

s1 is not found in this string.

count(substring): int

Returns the number of non-overlapping occurrences of this

substring.

>>> s = "welcome to python"
 >>> s.endswith("thon")
 True
 >>> s.startswith("good")
 False
 >>> s.find("come")
3
>>> s.find("become")
-1
>>> s.rfind("o")
17
>>> s.count("o")
3
>>>

String Conversion

endswith(s1: str): bool

Returns True if the string ends with the substring s1.

startswith(s1: str): bool

Returns True if the string starts with the substring s1.

find(s1): int

Returns the lowest index where s1 starts in this string, or –1 if

s1 is not found in this string.

rfind(s1): int

Returns the highest index where s1 starts in this string, or –1 if

s1 is not found in this string.

count(substring): int

Returns the number of non-overlapping occurrences of this

substring.

>>> s = "welcome to python"
>>> s1 = s.capitalize()
>>> s1
'Welcome to python'
>>> s2 = s.title()
 >>> s2
'Welcome To Python'
>>> s = "New England"
>>> s3 = s.lower()
 >>> s3
 'new england'
 >>> s4 = s.upper()
 >>> s4
 'NEW ENGLAND'
 >>> s5 = s.swapcase()
>>> s5
 'nEW eNGLAND'
 >>> s6 = s.replace("England", "Haven")
 >>> s6
 'New Haven'
 >>> s
 'New England'
 >>>
  • As stated earlier, a string is immutable. None of the methods in the str class changes
    the contents of the string; instead, these methods create new strings. As shown in the
    preceding script, s is still New England  after applying the methods
    s.lower(), s.upper(), s.swapcase(), and s.replace(“England”,
    “Haven”).