0% found this document useful (0 votes)
22 views2 pages

String

The document provides an overview of string slicing in Python, detailing the syntax and examples for extracting parts of strings. It also covers basic string methods for manipulation, searching, replacing, and testing strings. Key methods include str.upper(), str.find(), str.replace(), and various checks for character types.

Uploaded by

yashrajsk999
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)
22 views2 pages

String

The document provides an overview of string slicing in Python, detailing the syntax and examples for extracting parts of strings. It also covers basic string methods for manipulation, searching, replacing, and testing strings. Key methods include str.upper(), str.find(), str.replace(), and various checks for character types.

Uploaded by

yashrajsk999
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

1.

String Slicing in Python

Slicing allows you to extract parts of strings using a specific syntax.

Syntax:

string[start:stop:step]

start → index to start slicing (inclusive)

stop → index to stop slicing (exclusive)

step → how many steps to jump (optional)

Examples:

text = "Python Programming"

print(text[0:6]) # 'Python'

print(text[7:18]) # 'Programming'

print(text[:6]) # 'Python' (start is assumed 0)

print(text[7:]) # 'Programming' (goes till end)

print(text[::2]) # 'Pto rgamn' (every 2nd character)

print(text[::-1]) # 'gnimmargorP nohtyP' (reverse string)

Indexing:

Python uses zero-based indexing, and negative indices count from the end:

print(text[-1]) # 'g' (last character)

print(text[-3:]) # 'ing'

Basic Methods

MethodDescription

[Link]() Converts all characters to uppercase.

[Link]() Converts all characters to lowercase.

[Link]() Capitalizes the first letter.

[Link]() Capitalizes the first letter of each word.

[Link]() Removes leading and trailing whitespace.

[Link]() Removes whitespace from the start.

[Link]() Removes whitespace from the end.

Searching and Finding


MethodDescription

[Link](sub) Returns the first index of sub, or -1.

[Link](sub) Like find(), but raises an error if not found.

[Link](prefix) Checks if string starts with prefix.

[Link](suffix) Checks if string ends with suffix.

[Link](sub) Counts how many times sub occurs.

Rplacing and Splitting

MethodDescription

[Link](old, new) Replaces all occurrences of old with new.

[Link](sep) Splits string by sep into a list.

[Link](sep) Splits from the right.

[Link](list) Joins elements of a list with the string as a separator.

Testing Strings

MethodDescription

[Link]() True if all characters are letters.

[Link]() True if all characters are digits.

[Link]() True if alphanumeric (letters or digits).

[Link]() True if all characters are whitespace.

[Link]() True if all characters are uppercase.

[Link]() True if all characters are lowercase.

[Link]() True if in title case.

You might also like