to get part of a string in pyspark databricks

arkiboys 9,556 Reputation points

how is it possible to get the first 3 letters and the last 2 letters from a string such as:

value = 'feb-23'

I would like to get: firstPart = 'feb' and also get secondPart = '23'


Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
1,904 questions
0 comments No comments
{count} votes

Accepted answer
  1. MartinJaffer-MSFT 26,011 Reputation points

    @arkiboys Hi again, I'm back!

    To get the first 3 characters from a string, we can use the array range notation value[0:3] 0 means start 0 characters from the beginning, and 3 is end 3 characters from the beginning.

    To get the last 2 characters we get to use negative numbers! value[-2:] returns the last 2 characters. Don't do value[-2:0] , that won't give you anything. Alternatively can do value[ len(value) - 2 : len(value)].

    This gets you the characters, not specificly letters. If you want to differentiate letters and numbers and other symbols, a different tactic is needed.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful