Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:29:28+00:00 2026-06-08T23:29:28+00:00

As I’m French, I’m trying to make a little function that can add the

  • 0

As I’m French, I’m trying to make a little function that can add the good definite article before a country name. I have no problem except for the few countries that start with a diacritic. Here’s my code :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def article(nomPays):
    voyelles = ['A','E','É','I','O','U','Y']
    if nomPays == 'Mexique':
        return 'du'
    elif nomPays[0] in voyelles:
        return 'de l\''
    elif nomPays[-1] == 'e':#signe négatif pour compter à partir de la dernière lettre
        return 'de la'
    else:
        return 'du'

print article('Érythrée')

If I enter Allemagne instead of Érythrée, the behaviour is correct : it returns ‘de l”. But Érythrée returns ‘de la’. It means my function doesn’t recognize the character É as part of the voyelles list.

Can anyone explain me why and how I can resolve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:29:30+00:00Added an answer on June 8, 2026 at 11:29 pm

    The problem is that you’re using str in Python 2, where str is a sequence of bytes and so nomPays[0] will give the first byte of the string, not the first character. In single-byte encodings this isn’t a problem, but with multi-byte encodings like UTF-8 the first byte of “Érythrée” is a lead byte and not the whole character “É”.

    You need to change to use unicode to grab the first character:

    firstChar = unicode(nomPays, 'UTF-8')[0].encode('UTF-8')
    

    Actually, it’d probably be easier to use startswith:

    if any(nomPays.startswith(voyelle) for voyelle in voyelles):
    

    Alternatively you could use unicode throughout your application, or switch to Python 3, where all this is handled much better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.