Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Counting | String Methods
String Manipulation in Python

book
Counting

Let's consider some other methods that can be applied to strings. One of them can help us count the number of appearances of some symbol/substring within a string.

To count the number of symbol/substring in string use .count(symbol/substring) method. For example,

string = "String Manipulation in Python"
print("letter i appeared", string.count('i'), "times in the string.")
print("letter t appeared", string.count('t'), "times in the string.")
123
string = "String Manipulation in Python" print("letter i appeared", string.count('i'), "times in the string.") print("letter t appeared", string.count('t'), "times in the string.")
copy

Note

This method is case-sensitive to symbols. For the example above, string.count('m') will return 0, while string.count('M') will return 1.

Tehtävä

Swipe to start coding

You are given string "Introduction to Python". You need to calculate the number of symbols 'o' and 't' in this string.

Ratkaisu

# Variable
string = "Introduction to Python"

# Calculate number of letter 'o'
print(string.count('o'))
# Calculate number of letter 't'
print(string.count('t'))

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1
# Variable
string = "Introduction to Python"

# Calculate number of letter 'o'
print(string.___(___))
# Calculate number of letter 't'
print(___)

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt