JulienBeaulieu
  • Introduction
  • Sciences
    • Math
      • Probability
        • Bayes Rule
        • Binomial distribution
        • Conditional Probability
      • Statistics
        • Descriptive Statistics
        • Inferential Statistics
          • Normal Distributions
          • Sampling Distributions
          • Confidence Intervals
          • Hypothesis Testing
          • AB Testing
        • Simple Linear Regression
        • Multiple Linear Regression
          • Statistical learning course
          • Model Assumptions And How To Address Each
        • Logistic Regression
      • Calculus
        • The big picture of Calculus
          • Derivatives
          • 2nd derivatives
          • The exponential e^x
        • Calculus
        • Gradient
      • Linear Algebra
        • Matrices
          • Matrix Multiplication
          • Inverses and Transpose and permutations
        • Vector Space and subspaces
        • Orthogonality
          • Orthogonal Sets
          • Projections
          • Least Squares
        • Gaussian Elimination
    • Programming
      • Command Line
      • Git & GitHub
      • Latex
      • Linear Algebra
        • Element-wise operations, Multiplication Transpose
      • Encodings and Character Sets
      • Uncategorized
      • Navigating Your Working Directory and File I/O
      • Python
        • Problem Solving
        • Strings
        • Lists & Dictionaries
        • Storing Data
        • HTTP Requests
      • SQL
        • Basic Statements
        • Entity Relationship Diagram
      • Jupyter Notebooks
      • Data Analysis
        • Data Visualization
          • Data Viz Cheat Sheet
          • Explanatory Analysis
          • Univariate Exploration of Data
            • Bar Chart
            • Pie Charts
            • Histograms
            • Kernel Density Estimation
            • Figures, Axes, and Subplots
            • Choosing a Plot for Discrete Data
            • Scales and Transformations (Log)
          • Bivariate Exploration of Data
            • Scatterplots
            • Overplotting, Transparency, and Jitter
            • Heatmaps
            • Violin & Box Plots
            • Categorical Variable Analysis
            • Faceting
            • Line Plots
            • Adapted Bar Charts
            • Q-Q, Swarm, Rug, Strip, Stacked, and Rigeline Plots
          • Multivariate Exploration of Data
            • Non-Positional Encodings for Third Variables
            • Color Palettes
            • Faceting for Multivariate Data
            • Plot and Correlation Matrices
            • Other Adaptations of Bivariate PLots
            • Feature Engineering for Data Viz
        • Python - Cheat Sheet
    • Machine Learning
      • Courses
        • Practical Deep learning for coders
          • Convolutional Neural Networks
            • Image Restauration
            • U-net
          • Lesson 1
          • Lesson 2
          • Lesson 3
          • Lesson 4 NLP, Collaborative filtering, Embeddings
          • Lesson 5 - Backprop, Accelerated SGD
          • Tabular data
        • Fast.ai - Intro to ML
          • Neural Nets
          • Business Applications
          • Class 1 & 2 - Random Forests
          • Lessons 3 & 4
      • Unsupervised Learning
        • Dimensionality Reduction
          • Independant Component Analysis
          • Random Projection
          • Principal Component Analysis
        • K-Means
        • Hierarchical Clustering
        • DBSCAN
        • Gaussian Mixture Model Clustering
        • Cluster Validation
      • Preprocessing
      • Machine Learning Overview
        • Confusion Matrix
      • Linear Regression
        • Feature Scaling and Normalization
        • Regularization
        • Polynomial Regression
        • Error functions
      • Decision Trees
      • Support Vector Machines
      • Training and Tuning
      • Model Evaluation Metrics
      • NLP
      • Neural Networks
        • Perceptron Algorithm
        • Multilayer Perceptron
        • Neural Network Architecture
        • Gradient Descent
        • Backpropagation
        • Training Neural Networks
  • Business
    • Analytics
      • KPIs for a Website
  • Books
    • Statistics
      • Practice Statistics for Data Science
        • Exploring Binary and Categorical Data
        • Data and Sampling Distributions
        • Statistical Experiments and Significance Testing
        • Regression and Prediction
        • Classification
        • Correlation
    • Pragmatic Thinking and Learning
      • Untitled
    • A Mind For Numbers: How to Excel at Math and Science
      • Focused and diffuse mode
      • Procrastination
      • Working memory and long term memory
        • Chunking
      • Importance of sleeping
      • Q&A with Terrence Sejnowski
      • Illusions of competence
      • Seeing the bigger picture
        • The value of a Library of Chunks
        • Overlearning
Powered by GitBook
On this page
  • String methods
  • startswith() and endswith()
  • join()
  • split()
  • rjust() and ljust()
  • center()
  • strip(), rstrip(), lstrip()
  • replace()
  • pyperclip Module
  • Recap

Was this helpful?

  1. Sciences
  2. Programming
  3. Python

Strings

PreviousProblem SolvingNextLists & Dictionaries

Last updated 5 years ago

Was this helpful?

Escape characters, multiline strings, and raw strings (r'xyz').

Tripple quotes are handy if you have a gigantic string.

String methods

String methods return a new string instead of modifying the existing one.

isalpha() - letters only
'hello'.isalpha() -> True
'hello123'.isalpha() -> False

isalnum() - letters and number only

isdecimal() - numbers only

isspace() - whitespace only
'       '.isspace() -> True
'   Hello    World'.isspace() -> False
'Hello World'[5].isspace() -> True because [5] == ' '



istitle() - titelcase only 

You can also use 'hello world'.titre() -> 'Hello World'

startswith() and endswith()

'Hello World'.startswith('Hello') -> True
'Hello World!'.startswith('ello') -> False 

'Hello World'.endswith('World') -> True

join()

','.join(['cats', 'rats', 'bats'])
returns: 'cats,rats,bats'

'\n.join(['cats', 'rats', 'bats'])
cats
rats
bats

split()

Splits on white characters by default. The space is erased.

rjust() and ljust()

Justifies right and left. Adds padding. You can pass a 2nd argument to specify a character or string you want as a fill.

center()

Works like rjust() and ljust() but centers the text instead of justifying it.

strip(), rstrip(), lstrip()

Sometimes we might wanna just strip off white space, from the left side, right side of both sides of the string.

Ex: if there is too much space on the left side of the string.

strip() will remove white space from either side of the string. You can also pass the strip method a character you want to remove instead of just the white space.

There is however still spam in the middle. That's just because it removed those letters up to the first character that wasn't passed to strict method. And then the same on the inside it just went down to here. So this is the string that was returned in the replace string method takes two arguments a string to

replace()

pyperclip Module

The pyperclip module has copy and paste functions that can send text to and receive text from your computer's clipboard so sending the output of your program to the clipboard will make it really easy to paste it to an email or to a word processor software or some other program.

You need to install it with pip.

pyperclip.copy() and pyperclip.paste()

Recap