Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
83 lines (57 loc) · 1.02 KB

File metadata and controls

executable file
·
83 lines (57 loc) · 1.02 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

String methods

Examples in Python 3

"""
Transforming case
"""

# Capitalize
x = "hello"
y = x.capitalize()
print(y)

# Uppercase
y = x.upper()
print(y)

# Lowercase
x = "HeLLo"
y = x.lower()
print(y)


"""
Transforming encoding
"""
# Encode to unicode
x = "me@mail.com // 30€"
y = x.encode()
print(y)


"""
Replacing, splitting and joining
"""

# Replace parts
x = "Hello"
y = x.replace("l", "y")
print(y)

# Split
x = "I like trains"
y = x.split()
print(y)		# Iterable list retured from split

# Join
x = ["So", "long", "and", "thanks", "for", "all", "the", "fish"]
separator = " "
y = separator.join(x)
print(y)


"""
Searching a string
"""
x = "Applepie"
y = x.index("pie")
print(y)
print(x[y:])

y = x.find("pine")
print(y)		# Not found


Reference and read more

String Methods

Revision history

2014-07-17 (sylvanas) PA1 First try.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.