String in Python

0 Comments

Str.capitalize() returns a copy of the string containing first character capitalized and the rest in lowercased

x = "hello"

x.capitalize()

 

Output: Hello

Str.lower() returns a string in which all characters are in lowercased

x = "HELLO"

x.lower()

Output: hello

Str.upper() returns a string in which all characters are in uppercased

x= "hi uzair"

x.upper()

Output: HI UZAIR

Str.strip() returns a copy of string with leading and trailing characters removed. In this case white characters are removed

s = '   hello world   \n'

s.strip()

Output: hello world

Str.split() return a list of words of the string s

s = 'Lets split the words'
s.split(' ')

Output: [‘Lets’, ‘split’, ‘the’, ‘words’]

If we want to get a chunk or slice or substring from a  string we can do this by Slicing

word = "Howdy"

word[0:2]

Output: Ho

word[0:3]

Output: How

We can also find a substring in a string

'He' in word

Output: False

'How' in word

Output: True


Leave a Reply

Your email address will not be published.