0% found this document useful (0 votes)
2 views24 pages

Python String Basics and Methods

The document provides an overview of Python strings, highlighting their immutability and various methods for string manipulation such as indexing, slicing, and built-in functions like find(), replace(), and split(). It includes examples demonstrating how to create strings, check their properties, and modify them. Additionally, it explains string membership tests and methods for changing case and removing whitespace.

Uploaded by

Arpit Katiyar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Python String Basics and Methods

The document provides an overview of Python strings, highlighting their immutability and various methods for string manipulation such as indexing, slicing, and built-in functions like find(), replace(), and split(). It includes examples demonstrating how to create strings, check their properties, and modify them. Additionally, it explains string membership tests and methods for changing case and removing whitespace.

Uploaded by

Arpit Katiyar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String in Python

Learn by computer shagoofi

SUBSCRIBE IT TO WATCH MORE VIDEOS


Why are Python Strings Immutable?
• Strings in Python are “immutable” which means they can not be changed
after they are created. Some other immutable data types are integers,
float, boolean, etc.
• message = 'Hola Amigos’
message[0] = 'Q’
• print(message)
• message = 'Hola Amigos'

• # assign new string to message variable


• message = 'Hello Friends'

• print(message); # prints "Hello Friends"

SUBSCRIBE IT TO WATCH MORE VIDEOS


Creating String in Python

#Using single quotes


str1 = 'Hello Python'
print(str1)
#Using double quotes
str2 = "Hello Python"
print(str2)

#Using triple quotes


str3 = '''Triple quotes are generally used for
represent the multiline or
doc string'''
print(str3)

SUBSCRIBE IT TO WATCH MORE VIDEOS


Methods of Python String
Methods Description Example
[Link]() Return a copy of the string with its first character “i love my India .capitalize()
capitalized #I love my India
[Link](sub,[,start[,end]]) Returns the lowest index in the string where the
substring sub is found within the slice range of start and
end.
Return -1 substring is not found
[Link]() Return True if the character in the string are string="abc123"
alphanumeric (alphabets or numbers) and there is at str="hello"
least one character, False otherwise. num="123"
special="@"
print([Link]())#true
print([Link]())#true
print([Link]())#true
print([Link]())#false

[Link]() Return True if the character in the string are alphabetic #isalpha()
) and there is at least one character, False otherwise. print([Link]())#false
print([Link]())#true

SUBSCRIBE IT TO WATCH MORE VIDEOS


Methods of Python String
Method Description Example

[Link]() Return True if all the characters is in the #isdigit()


string are digits. There must be at least one print([Link]())#false
character, otherwise it returns False. print([Link]())#true

[Link]() Return True if all cased character in the string #islower()


are lowercase. There must be at least one print([Link]())#true
cased character. It return False otherwise. print([Link]())#false

[Link]() Return true if there are only whitespaces #isspace()


character in the string s=" python "
print([Link]())#false
s=" "
print([Link]())#true

[Link]() Return True if all cased character in the string #isupper()


are uppercase. There must be at least one print([Link]())#false
cased character. It return False otherwise.

SUBSCRIBE IT TO WATCH MORE VIDEOS


Methods of Python String
• Methods Description
• upper() Converts the string to uppercase
• lower() Converts the string to lowercase
• partition() Returns a tuple
• replace() Replaces substring inside
• rstrip() Removes trailing characters
• split() Splits string from left
• startswith() Checks if string starts with the specified string
• isnumeric() Checks numeric characters
• index() Returns index of substring

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python String Length
• greet = 'Hello'

• # count length of greet string


• print(len(greet))

SUBSCRIBE IT TO WATCH MORE VIDEOS


Access String Characters in Python
• Indexing: One way is to treat strings as a list and use index values. For
example,
• greet = 'hello'
• # access 1st index element
• print(greet[1]) # "e"
• Negative Indexing: Similar to a list, Python allows negative indexing
for its strings. For example,
• greet = 'hello'
• # access 4th last element
• print(greet[-4]) # "e“

SUBSCRIBE IT TO WATCH MORE VIDEOS


Slicing
• Slicing: Access a range of characters in a string by using the slicing
operator colon :. For example,
• greet = 'Hello'
• # access character from 1st index to 3rd index
• print(greet[1:4]) # "ell"

SUBSCRIBE IT TO WATCH MORE VIDEOS


String Membership Test

• We can test if a substring exists within a string or not, using the


keyword in.
• print('a' in 'program') # True
• print('at' not in 'battle') # False

SUBSCRIBE IT TO WATCH MORE VIDEOS


replace()
• text = 'bat ball'

• # replace 'ba' with 'ro'


• replaced_text = [Link]('ba', 'ro')
• print(replaced_text)

• # Output: rot roll

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python String find()

• The find() method returns the index of first occurrence of the


substring (if found). If not found, it returns -1.
• message = 'Python is a fun programming language'
• # check the index of 'fun'
• print([Link]('fun'))

SUBSCRIBE IT TO WATCH MORE VIDEOS


find() Return Value

• The find() method returns an integer value:


• If the substring exists inside the string, it returns the index of the first
occurence of the substring.
• If a substring doesn't exist inside the string, it returns -1.

SUBSCRIBE IT TO WATCH MORE VIDEOS


find() With start and end Arguments

• quote = 'Do small things with great love'

• # Substring is searched in ‘things with great love'


• print([Link]('small things', 10))

• # Substring is searched in ' small things with great love'


• print([Link]('small things', 2))

• # Substring is searched in 'hings with great lov'


• print([Link]('o small ', 10, -1))

• # Substring is searched in 'll things with'


• print([Link]('things ', 6, 20))

SUBSCRIBE IT TO WATCH MORE VIDEOS


Example: Python String split()
• text= 'Split this string'

• # splits using space


• print([Link]())

• grocery = 'Milk, Chicken, Bread'

• # splits using ,
• print([Link](', '))

• # splits using :
• # doesn't split as grocery doesn't have :
• print([Link](':'))

SUBSCRIBE IT TO WATCH MORE VIDEOS


Example: split() with maxsplit
grocery = 'Milk#Chicken#Bread#Butter'

# maxsplit: 1
print([Link]('#', 1))

# maxsplit: 2
print([Link]('#', 2))

# maxsplit: 5
print([Link]('#', 5))

# maxsplit: 0
print([Link]('#', 0))

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python String startswith()

• message = 'Python is fun'

• # check if the message starts with Python


• print([Link]('Python'))

• # Output: True

SUBSCRIBE IT TO WATCH MORE VIDEOS


How Python title() works?
• text = 'My favorite number is 25.'
• print([Link]())

• text = '234 k3l2 *43 fun'


• print([Link]())

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python - Modify Strings

• Changing Case
• One of the simplest ways to modify a string is by changing its case using
[Link]() method which converts all characters in the string to uppercase.
• s = "Shagoofi Anjum"
• print([Link]())
• Python String lower() Method
• s = "Shagoofi Anjum"

• # Change all uppercase letters to lowercase


• res = [Link]()
• print(res)

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python String lstrip() Method
• The lstrip() method removes leading whitespace characters from a
string. We can also specify custom characters to remove from the
beginning/starting of the string.
•s=" Shagoofi Anjum"
• res = [Link]()
• print(res)

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python String partition() Method
• string = "Python is fun"

• # 'is' separator is found


• print([Link]('is '))

• # 'not' separator is not found


• print([Link]('not '))

• string = "Python is fun, isn't it"

• # splits at first occurence of 'is'


• print([Link]('is'))

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python String replace() Method
• s = "byee C! Hello Python!"

• # Replace "byee" with "Hi"


• s1 = [Link]("Hello", "Hi")

• print(s1)

SUBSCRIBE IT TO WATCH MORE VIDEOS


Python String index() Method
• index() With Substring argument Only
sentence = 'Python programming is fun.'

result = [Link]('is fun')


print("Substring 'is fun':", result)

result = [Link]('Java')
print("Substring 'Java':", result)

SUBSCRIBE IT TO WATCH MORE VIDEOS


Thank you
SUBSCRIBE IT TO WATCH MORE VIDEOS

You might also like