0% found this document useful (0 votes)
23 views6 pages

Python String Functions & Methods Guide

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)
23 views6 pages

Python String Functions & Methods Guide

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

Table of Contents

1. Introduction ............................................... 1

2. What are Strings in Python? .................................. 2

3. String Functions vs String Methods ........................... 3

4. Common String Functions ...................................... 4


4.1 len()
4.2 str()
4.3 format()
4.4 ord() and chr()
4.5 max(), min()
5. Important String Methods .................................... 5
5.1 Case Conversion (upper(), lower(), title(), capitalize())
5.2 Searching & Finding (find(), index(), count())
5.3 Modifying Strings (replace(), strip(), lstrip(), rstrip())
5.4 Splitting & Joining (split(), rsplit(), join())
5.5 Checking Content (isalpha(), isdigit(), isalnum(), isspace())
5.6 Alignment (center(), ljust(), rjust(), zfill())
6. Examples of String Functions and Methods .................... 6

7. Applications in Real-World Programming ...................... 7

8. Conclusion .................................................. 8

9. References .................................................. 9

Seminar on Python String Functions and


Methods

1. Introduction
Python is one of the most widely used programming languages in the world. It is
simple, versatile, and has extensive support for data types and string
manipulation. Strings are one of the most important data types in Python because
almost every program deals with text processing, such as reading input, displaying
output, formatting reports, or handling user data.
Python provides a rich set of functions and methods to manipulate and process
strings easily, making it very powerful for developers, students, and researchers.
2. What are Strings in Python?
A string in Python is a sequence of characters enclosed within single quotes ('),
double quotes ("), or triple quotes (''' or """).
Example:
name = "Python"
greeting = 'Hello'
paragraph = """This is a string
that spans multiple lines."""
Strings are immutable, meaning once a string is created, it cannot be changed.
However, you can create a new string based on modifications.

3. String Functions vs String Methods


 String Functions: These are built-in global functions in Python that work
on strings (e.g., len(), max(), min(), str()).
 String Methods: These are functions that are called on a string object
itself using dot (.) notation (e.g., "hello".upper(), "text".replace("t","T")).

4. Common String Functions


4.1 len()
Returns the length of a string.
len("Python") # Output: 6
4.2 str()
Converts other data types into string.
str(123) # Output: '123'
4.3 format()
Used for string formatting.
"Hello, {}".format("World") # Output: "Hello, World"
4.4 ord() and chr()
 ord() → Returns Unicode code of a character.
 chr() → Returns character for a Unicode number.
ord('A') # Output: 65
chr(97) # Output: 'a'
4.5 max() and min()
Finds the maximum or minimum character in a string (based on Unicode).
max("apple") # Output: 'p'
min("apple") # Output: 'a'

5. Important String Methods


5.1 Case Conversion
 upper() → Converts string to uppercase.
 lower() → Converts string to lowercase.
 title() → Capitalizes first letter of each word.
 capitalize() → Capitalizes only the first letter.
Example:
"python".upper() # Output: 'PYTHON'
"HELLO".lower() # Output: 'hello'
"python programming".title() # Output: 'Python Programming'

5.2 Searching & Finding


 find(substring) → Returns index of first occurrence (or -1).
 index(substring) → Same as find(), but raises error if not found.
 count(substring) → Counts occurrences of substring.
text = "hello world"
[Link]("world") # Output: 6
[Link]("l") # Output: 3

5.3 Modifying Strings


 replace(old, new) → Replaces occurrences.
 strip() → Removes leading/trailing whitespace.
 lstrip() → Removes from left side.
 rstrip() → Removes from right side.
" hello ".strip() # Output: 'hello'
"banana".replace("a", "o") # Output: 'bonono'

5.4 Splitting & Joining


 split() → Breaks string into list.
 rsplit() → Splits from right side.
 join() → Joins list elements into string.
"one two three".split() # Output: ['one', 'two', 'three']
",".join(['a', 'b', 'c']) # Output: 'a,b,c'

5.5 Checking Content


 isalpha() → True if all letters.
 isdigit() → True if only digits.
 isalnum() → True if alphanumeric.
 isspace() → True if only whitespace.
"123".isdigit() # True
"abc".isalpha() # True
"abc123".isalnum() # True

5.6 Alignment
 center(width, char) → Centers string.
 ljust(width, char) → Left-justifies.
 rjust(width, char) → Right-justifies.
 zfill(width) → Adds zeros in front.
"42".zfill(5) # Output: '00042'

6. Examples of String Functions and Methods


s = " Python Programming "

print(len(s)) # 20
print([Link]()) # "Python Programming"
print([Link]()) # " PYTHON PROGRAMMING "
print([Link]("Pro")) #8
print([Link]("Python","Java")) # " Java Programming "
print(",".join(["A","B","C"])) # "A,B,C"

7. Applications in Real-World Programming


 Data cleaning: Removing unwanted spaces, converting cases.
 User input validation: Checking if input is digits, alphabets, or alphanumeric.
 Natural language processing (NLP): Splitting, tokenizing, and formatting
text.
 Report generation: Formatting strings for tables and structured outputs.
 File handling: Parsing file contents into meaningful data.

8. Conclusion
Python provides a powerful set of string functions and methods that make text
processing simple and efficient. These features are heavily used in data analysis,
web development, machine learning, and everyday applications. By mastering
these operations, programmers can handle any kind of textual data with ease.

9. References
1. Python Software Foundation – Python Official Documentation:
[Link]
2. Mark Lutz, Learning Python, O’Reilly Media.
3. Dr. Charles Severance, Python for Everybody.
4. Al Sweigart, Automate the Boring Stuff with Python.
5. Real Python Tutorials – Working with Strings:
[Link]

You might also like