0% found this document useful (0 votes)
2 views

strings and string methods

This document provides an overview of strings and string methods in Python, including definitions, indexing, slicing, concatenation, replication, and traversing. It also covers various string methods such as upper(), lower(), join(), split(), and isX() methods, along with examples for each. Additionally, it includes programming exercises for checking palindromes and reversing words in a string.

Uploaded by

jnchandan07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

strings and string methods

This document provides an overview of strings and string methods in Python, including definitions, indexing, slicing, concatenation, replication, and traversing. It also covers various string methods such as upper(), lower(), join(), split(), and isX() methods, along with examples for each. Additionally, it includes programming exercises for checking palindromes and reversing words in a string.

Uploaded by

jnchandan07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Revision Class2

1. Strings and String Methods ( Class 2)


Sl.No. Questions
1. What is a string
◦ String is a data type in Python
◦ It represents a sequence of characters enclosed within quotes
Examples:
>>>college = ‘Sai Vidya Institute of Technology’
>>>message = “Good morning”
2. Indexing
• Indexing are of 2 type: Positive Indexing and Negative Indexing

S = “Python”

Positive 0 1 2 3 4 5
Indexing
P y t h o n
Negative -6 -5 -4 -3 -2 -1
Indexing

>>> s = “Python”

>>>s[0]
‘P’
>>>s[1]
‘y’
>>>s[-1]
‘n’
>>>s[-4]
‘t’
3. slicing
Slicing help us to get substring.

>>> name = ‘PYTHON’


>>> name[0 : 6]
‘PYTHON’
>>>name[2 :]
‘THON’
>>>name[ : : 2]
‘PTO’
>>> name = ‘PYTHON’
>>> name[-6 : -4 ]
‘PY’
>>>name[-4 : ]
‘THON’
>>>name[ : : -1]
‘NOHTYP’
4. string concatenation and string replication
String Concatenation
Two strings can be concatenated using a + operator

>>> ‘blue’ + ‘berry’


‘blueberry’

String Replication
A string can be replicated using * operator

>>> ‘Alice’ * 3
‘AliceAliceAlice’
5. String Traversing
Visiting every character in a string using a for loop is called string traversing
Examples 1:
for i in “PYTHON”:
print(i)
OUTPUT:
P
Y
T
H
O
N
Examples 2:
S = “PYTHON”
for i in range(len(S)):
print(S[i])
OUTPUT:
P
Y
T
H
O
N
6. String Methods
upper() upper() method returns the string in upper case letters

>>> s = “Good Morning”


>>> s.upper()

“GOOD MORNING”
lower() lower() method returns the string in lower case letters
>>> s = “Good Morning”
>>> s.lower()

“good moring”

join() join() method takes list of strings as argument that need to be joined
together into a single string value.

>>> ' '.join(['My', 'name', 'is', 'Simon'])


'My name is Simon'
split() The split() method is called on a string value and returns a list of strings.

>>> 'My name is Simon'.split()


['My', 'name', 'is', 'Simon’]

partition() The partition() string method can split a string into the text before and
after a separator string.
>>> ‘coding is fun’.partition('is')
('coding ', 'is', 'fun')

rjust() The rjust() and ljust() string methods return a padded version of the string
they are called on, with spaces inserted to justify the text
ljust()
>>>'Hello'.rjust(10)
center() ' Hello'

>>>'Hello'.ljust(10)
'Hello '

>>>’Hello’.center(10)

' Hello '


>>>'Hello'.rjust(10, ‘*’)
'*****Hello'

>>>'Hello'.ljust(10,’*’)
'Hello*****'

>>>’Hello’.center(10, ‘*’)

'**Hello***'

strip() • The strip() string method will return a new string without any
whitespace characters at the beginning or end.
lstrip() • The lstrip() and rstrip() methods will remove whitespace characters
from the left and right ends, respectively.
rstrip()
>>> spam = ' Hello, World '

>>> spam.strip()
'Hello, World'

>>> spam.lstrip()
'Hello, World '

>>> spam.rstrip()
' Hello, World'

7. Explain the isX() methods

islower() returns a Boolean True value if the string has at least one letter
and all the letters are lowercase. Otherwise, returns False

>>> spam = 'Hello, world!'


>>> spam.islower()
False

isupper() returns a Boolean True value if the string has at least one letter
and all the letters are uppercase. Otherwise, returns False

>>> 'HELLO'.isupper()
True

isalpha() Returns True if the string consists only of letters


>>> 'hello'.isalpha()
True
isalnum() Returns True if the string consists only of letters and numbers
>>> 'hello123'.isalnum()
True
isdecimal() Returns True if the string consists only of numeric characters
>>> '123'.isdecimal()
True
isspace() Returns True if the string consists only of spaces, tabs, and
newlines
>>> ' '.isspace()
True
Istitle() Returns True if the string consists only of words that begin with
an uppercase letter followed by only lowercase letters
>>> 'This Is Title Case'.istitle()
True
8. Develop a python program to determine whether the given string is a palindrome or not a
palindrome.

s = input(“Enter a string:”)

if s == s[: : -1] :
print(“String is a palindrome”)
else:
print(“String is not a palindrome”

9. Using string slicing operation write python program to reverse each word in a given string (eg:
input: “hello how are you”, output: “olleh woh era uoy”)

You might also like