How do I print a specific character from a string in Python? I am still learning and now trying to make a hangman like program. The idea is that the user enters one character, and if it is in the word, the word will be printed with all the undiscovered letters as "-".
I am not asking for a way to make my idea/code of the whole project better, just a way to, as i said, print that one specific character of the string.
asked Jan 31, 2016 at 16:54
2
print[yourstring[characterposition]]
Example
print["foobar"[3]]
prints the letter b
EDIT:
mystring = "hello world"
lookingfor = "l"
for c in range[0, len[mystring]]:
if mystring[c] == lookingfor:
print[str[c] + " " + mystring[c]];
Outputs:
2 l
3 l
9 l
And more along the lines of hangman:
mystring = "hello world"
lookingfor = "l"
for c in range[0, len[mystring]]:
if mystring[c] == lookingfor:
print[mystring[c], end=""]
elif mystring[c] == " ":
print[" ", end=""]
else:
print["-", end=""]
produces
--ll- ---l-
answered Jan 31, 2016 at 16:58
J. TitusJ. Titus
9,2371 gold badge31 silver badges44 bronze badges
2
all you need to do is add brackets with the char number to the end of the name of the string you want to print, i.e.
text="hello"
print[text[0]]
print[text[2]]
print[text[1]]
returns:
h
l
e
answered Nov 8, 2017 at 18:39
Well if you know the character you want to search you can use this approach.
i = character looking for
input1 = string
if i in input1:
print[i]
you can change the print statement according to your logic.
answered Jun 6, 2021 at 8:05
name = "premier league"
for x in name:
print[x]
Result shown below:-
To print specific characters of the given string. For example to print 'l' from the given string
name = "premier league"
for x in name:
if x == "l":
print["element found: "+x]
answered Feb 3 at 12:12
1
Print specific character[s] in a string in Python #
Use string slicing to print specific characters in a string, e.g. print[my_str[:5]]
. The print[]
function will print the specified character or slice of the string to the terminal.
Copied!
my_str = 'bobbyhadz.com' # ✅ print first character in string print[my_str[0]] # 👉️ 'b' # ✅ print second character in string print[my_str[1]] # 👉️ 'o' # ✅ print last character in string print[my_str[-1]] # 👉️ 'm' # ✅ print first 5 characters in string print[my_str[:5]] # 👉️ 'bobby' # ✅ print last 5 characters in string print[my_str[-5:]] # 👉️ 'z.com' # ✅ print each character and its index in string for index, char in enumerate[my_str]: print[index, char]
The first 3 examples access a specific character at its index and print the result.
Python indexes are zero-based, so the first character in a string has an index of 0
, and the last character has an index of -1
or len[my_str] - 1
.
Copied!
my_str = 'bobbyhadz.com' print[my_str[0]] # 👉️ 'b' print[my_str[1]] # 👉️ 'o' print[my_str[-1]] # 👉️ 'm'
Negative indices can be used to count backwards, e.g. my_str[-1]
returns the last character in the string and my_str[-2]
returns the second-to-last character.
The
print function takes one or more objects and prints them to sys.stdout
.
Note that the print[]
function returns None
, so don't try to store the result of calling print
in a variable.
Instead, store the expression in a variable and print the result.
Copied!
my_str = 'bobbyhadz.com' first = my_str[0] print[first] # 👉️ 'b'
If you try to access an index that is not present in the string, an
IndexError
is raised.
You can use a try/except
statement if you need to handle the error.
Copied!
my_str = 'bobbyhadz.com' try: print[my_str[100]] except IndexError: # 👇️ this runs print['Specified index out of range']
If you need to print multiple characters in a string, use string slicing.
Copied!
my_str = 'bobbyhadz.com' # ✅ print first 5 characters in string print[my_str[:5]] # 👉️ 'bobby' # ✅ print last 5 characters in string print[my_str[-5:]] # 👉️ 'z.com'
The syntax for string slicing is my_str[start:stop:step]
.
The start
index is inclusive, whereas the stop
index is exclusive [up to, but not including].
If the start
index is not specified, the slice starts at index 0
.
If the stop
index is not
specified, the slice starts at the specified index and continues to the end of the string.
If you need to get a slice somewhere in the middle of the string, specify the start
and stop
indexes.
Copied!
my_str = 'bobbyhadz.com' start_index = my_str.index['h'] stop_index = my_str.index['.'] print[my_str[start_index:stop_index]] # 👉️ hadz
The str.index method returns the index of the first occurrence of the provided substring in the string.
The method raises a ValueError
if the substring
is not found in the string.
Note that the stop
index is exclusive [up to, but not including].
If you need to print the characters in a string and the corresponding index, use the enumerate[]
function.
Copied!
my_str = 'bobbyhadz.com' for index, char in enumerate[my_str]: print[index, char] # 👉️ 0 b, 1 o, 2 b, 3 b...
The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.