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

String Functions

Python string functions with egs

Uploaded by

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

String Functions

Python string functions with egs

Uploaded by

senpaistudioyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Stri Length
ng
Thelen() function returns
length
the of a string.

#Program to findlength
the of a string

= "Hello, World!"
string

= len(string)
length

print("The
length of the string is: {length}")

Output:

Thelength of the string is: 13

2. String Concatenation

You can concatenate (join) two strings using the + operator.

# Program to concatenate two strings

str1 "Hello"
=

str2= "World"

= str1 + " str2


result "+

print("Concatenated string:", result)

Output:

Hello World
Concatenated string:
3. String Case Conversion

You can use upper()lower()


and functions to convert strings to uppercase
lowercase.
or

# Program to convert a string to uppercase


lowercase
and

string = "Python Programming"

print("Original string:", string)

print("Uppercase:", string.upper())

print("Lowercase:", string.lower())

Output:

Original string: Python Programming

PROGRAMMING
Uppercase: PYTHON

Lowercase: python programming

4.String Slicing

Slicing allows you to extract a portion of a string.

# Program to slice a string

= "Hello, World!"
string

sliced_string = string[0:5]
# Extract characters from index 0 to 4

print("Sliced string:", sliced_string)

Output:

Hello
Sliced string:
5. String Replace

The replace() function replaces a substring within a string.

# Program to replace a substring

= "Ilove Python programming!"


string

new_string = string.replace("Python", "Java")

print("Modified string:", new_string)

Output:

Modified string: I Java programming!


love

6. String Strip

The strip(),
lstrip(), and rstrip() functions remove whitespace from the beginning or end o
string.

# Program to strip whitespace from a string

= " Hello, World! "


string

print("Original string:", repr(string))

print("After strip:", repr(string.strip()))

print("After
lstrip:", repr(string.lstrip()))

print("After rstrip:", repr(string.rstrip()))

Output:

Original
string:
' Hello, World! '

After strip:
'Hello, World!'

'Hello, World!
Afterlstrip: '

After rstrip:
' Hello, World!'
7. Check Substring

The in operator checks if a substring exists within a string.

# Program to check if a substring is present

= "Hello, World!"
string

if "World" in string:

print("The substring 'World' is present in the string.")

else:

print("The substring 'World' is not present in the string.")

Output:

The substring 'World' is present in the string.

8. String Split and Join

The split() function splits a string


list,
into
and
a join() combines
list into
a a string.

# Program to split and join strings

string = "Python is fun"

split_string = string.split()
# Split by spaces

print("Splitted string:", split_string)

joined_string = "-".join(split_string)
# Join with a hyphen

print("Joined string:", joined_string)

Output:

Splitted string: 'is',fun']


['Python', '

Joined string: Python-is-fun


9. String
Find

The find() function returns the index of the first occurrence of aifsubstring,
not found.or -1

# Program to find a substring

= "Hello, World!"
string

position = string.find("World")

if position
!= -1:

print(f"Substring 'World' found at index {position}.")

else:

print("Substring 'World' not found.")

Output:

Substring 'World' found at index 7.

10. String Count

The count() function returns the number of occurrences of a substring.

# Program to count occurrences of a substring

string = "banana"

count = string.count("a")

print(f"The
letter 'a' appears {count} times in the word 'banana'.")

Output:

Theletter 'a' appears 3 times in the word 'banana'.

You might also like