LogoLogo
Computing Skills
Computing Skills
  • Introduction
  • File Directory Structures
  • Text Editors
  • GitHub
  • Unix
  • Julia
    • Installation
    • REPL
    • Basic Syntax
    • Numbers and Math
    • Strings and Characters
    • Regular Expressions
    • Control Flow
    • Collections and Data Structures
    • File Input/Output
    • Packages
    • DataFrames
    • JuliaPlots
    • ScikitLearn.jl
    • JuliaStats
    • Exercises
  • Python
    • Installation
    • REPL
    • Basic Syntax
    • Numbers and Math
    • Strings and Characters
    • Regular Expressions
    • Control Flow
    • Collections and Data Structures
    • File Input/Output
    • Packages
    • Data Frames and Data Manipulation
  • R
    • Installation
    • REPL
    • Basic Syntax
    • Numbers and Math
    • Strings and Characters
    • Regular Expression
    • Control Flow
    • Collections and Data Structures
    • File Input/Output
    • Packages
    • DataFrames
    • Data Analysis and Manipulation
Powered by GitBook
On this page
  • Strings
  • Some functions and index methods that can be performed on strings
  • Resources
Export as PDF
  1. Python

Strings and Characters

This page provides syntax for different data types in Python as well as some of their associated functions. Each section includes an example to demonstrate the described syntax or function.

Strings

  • A string is a sequence of one or more characters (index values start at 0)

Some functions and index methods that can be performed on strings

Action
Function

get word length

len("abc")

extract nth character from word

"abc"[n]

extract substring nth-mth character from word

"abc"[n:m]

search for character in word

"abc".index("character")

search for subword in word

"ab" in "abc"

remove white spaces from the end of a word

"abc ".strip()

remove last character from word

"abc"[:-1]

determine data structure type

type("abc")

Input:

# strings.py

letter = "b"
word = "good-bye"
subword = "good"

word_length = len(word)
word_first_char = word[0]
word_subword = word[5:8]

print(f"Length of word: {word_length}")
print(f"First letter: {word_first_char}")
print(f"Last three characters: {word_subword}")

print(f"{letter} is in {word}: {(word.index(letter))}")
print(f"{subword} is in {word}: {(subword in word)}")
print(f"remove the last character: {(word[:-1])}")

Output:

Length of word: 8
First character: g
Last three characters: bye
b is in good-bye: 5
good is in good-bye: True
chop off the last character: good-by

Resources

PreviousNumbers and MathNextRegular Expressions

Last updated 6 months ago

W3 Schools:

Python Strings