Python Forum
How to get first two characters in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get first two characters in a string
#2
You are getting lists when you want to get elements.
a = ['one', 'two', 'three']
b = a[0:1] # Gets a list
c = b[0:2] # Gets a list
x = a[0]   # Gets a string from the list
y = x[:2]  # Gets a substring from the string
print(b, c, x, y)
Output:
['one'] ['one'] one on
Reply


Messages In This Thread
RE: How to get first two characters in a string - by deanhystad - May-19-2020, 10:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract continuous numeric characters from a string in Python Robotguy 2 545 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 710 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  Remove escape characters / Unicode characters from string DreamingInsanity 5 3,162 May-15-2020, 01:37 PM
Last Post: snippsat
  Replacing characters in a string with a list cjms981 1 788 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Strange Characters in JSON returned string fioranosnake 4 1,876 Dec-02-2019, 07:25 PM
Last Post: fioranosnake
  Split a long string into other strings with no delimiters/characters krewlaz 4 991 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  How to iterate over some characters in a string and the others will stay as it is. ? sodmzs 9 1,704 Jun-17-2019, 06:45 PM
Last Post: perfringo
  modify the color a string of characters in python 3.6 atlass218 10 2,563 Feb-28-2019, 03:20 PM
Last Post: atlass218
  String being broken up into single characters ammorgan 1 1,046 Dec-31-2018, 07:06 AM
Last Post: Gribouillis
  clean unicode string to contain only characters from some unicode blocks gmarcon 2 1,835 Nov-23-2018, 09:17 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020