Skip to main content

String Method

What is a method?

A method is a function that is associated with a class.

This might not make sense, but basically it is a feature we can use to do something with a data type.

For example, we can use the .split method to split a string into pieces.

Here, the .split is a method to split a string data

Lowercase and Uppercase methods

It is very important to know about these methods.

In English and a few other languages, the lowercase and uppercase is a very important difference.

To make sure we can handle these differences, we need to turn one into the other.

Usually, uppercase is turned to a lower case with the lower() method.

print("HELLO".lower())

These kinds of method is usually used to handle inputs.

Other uper/lower methodsExplanation
title()To a title-format (Make every character in front of a word uppercase)
upper()Transform into an uppercase
isupper()Check if the string is uppercase
islower()Check if the string is lowercase

len()

The len() method is used for checking how long a string is.

print(len("Hello"))

len is the shortened word for 'length'.

index()

The index() method will help you find where a 'substring' is located.

Substring is a part of a string, and the index is the position of the substring.

print('abcdef'.index('abc')) # prints 0

If there is no substring, it will throw an error.

Operators

Operators are used to perform mathematical operations.

In strings, they are used like a method shortcut.

in

This operator returns a Boolean of whether a substring exists in a string.

print('abc' in 'abcdef') # prints True

+

This operator is used to concatenate two strings.

print('abc' + 'def') # prints abcdef

Concatenate means to combine together.

*

This operator is used to repeat a string.

print('abc' * 3) # prints abcabcabc

Python Quiz 6

3 Questions
문자열 메서드 연습